arr=[1,2,3]; function modi(str){ switch(str){ case 1: str = "a"; break; case 2: str = "b"; break; case 3: str = "c"; break; } } modi(arr[0]); modi(arr[1]); modi(arr[2]); console.log(arr);//返回 1,2,3 要怎样才能返回 a,b,c
1 回答

红颜莎娜
TA贡献1842条经验 获得超12个赞
因为js的传参都只是值传递,所以你只是传个值进去自然不可能修改到外层的数组。
方法有很多,最简单的在函数最后return str,然后用arr[0] = modi(arr[0])的方式调用。
添加回答
举报
0/150
提交
取消