这是我想做的一个例子
function test(r){
var arr = ['d','e','f'];
r.push(arr);
/*
More Code
*/
return r;
}
var result = test(['a','b','c']);
alert(result.length);//I want this to alert 6
Run Code Online (Sandbox Code Playgroud)
我需要做的是传入一个数组并将其他数组附加到它的末尾然后返回数组.因为通过参考我不能使用array.concat(array2);.有没有办法做到这一点,而不使用像for循环这样的东西逐个添加元素.我尝试了类似的东西r.push(arr.join());但是也没用.此外,我想在数组中有对象的选项,所以真的r.push(arr.join());不能很好地工作.
小智 51
>>> var x = [1, 2, 3], y = [4, 5, 6];
>>> x.push.apply(x, y) // or Array.prototype.push.apply(x, y)
>>> x
[1, 2, 3, 4, 5, 6]
Run Code Online (Sandbox Code Playgroud)
或者使用解构,您现在可以执行此操作
//generate a new array
a=[...x,...y];
//or modify one of the original arrays
x.push(...y);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11007 次 |
| 最近记录: |