追加数组在JavaScript中另一个数组 [英] Append an array to another array in JavaScript

查看:129
本文介绍了追加数组在JavaScript中另一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

这个问题是完全相同的副本:结果
  <一href=\"http://stackoverflow.com/questions/1374126/how-to-append-an-array-to-an-existing-javascript-array\">How到一个数组附加到现有的JavaScript数组?


你如何追加数组在JavaScript中另一个阵列?

其他的方式,一个人可以一句话这样一个问题:


  • 阵列添加到另一个

  • 的毗连/并置阵列

  • 扩展的阵列,另一个数组

  • 把一个阵列中的内容到另一个数组

我花了一些时间寻找这个问题的答案。有时候,这样的最简单的是最难找到答案,所以我在这里希望用大量的关键词和短语按本的博客文章。请随时回答这个问题与任何其他有用的信息或编辑下面的关键词和短语。


解决方案

如果要修改,而不是返回一个新的数组原来的数组,使用 .push() ...

  array1.push.apply(数组1,数组2);
array1.push.apply(数组1,ARRAY3);

我用。适用来阵列的各个成员推 2 3 一次。

或...

  array1.push.apply(数组1,array2.concat(ARRAY3));


要处理大数组,可以分批做到这一点。

 为(VAR N = 0,to_add = array2.concat(ARRAY3); N&LT; to_add.length; N + = 300){
    array1.push.apply(数组1,to_add.slice(N,N + 300));
}


如果你这样做了很多,创造一个方法或函数来处理它。

  VAR push_apply = Function.apply.bind([]推。);
VAR slice_call = Function.call.bind([]切片。);Object.defineProperty(Array.prototypepushArrayMembers,{
    值:函数(){
        对于(VAR I = 0; I&LT;与arguments.length;我++){
            VAR to_add =参数[I]
            对于(VAR N = 0; N&LT; to_add.length; N + = 300){
                push_apply(此,slice_call(to_add,N,N + 300));
            }
        }
    }
});

和使用这样的:

  array1.pushArrayMembers(数组2,ARRAY3);

\r
\r

VAR push_apply = Function.apply.bind([]推。);\r
VAR slice_call = Function.call.bind([]切片。);\r
\r
Object.defineProperty(Array.prototypepushArrayMembers,{\r
    值:函数(){\r
        对于(VAR I = 0; I&LT;与arguments.length;我++){\r
            VAR to_add =参数[I]\r
            对于(VAR N = 0; N&LT; to_add.length; N + = 300){\r
                push_apply(此,slice_call(to_add,N,N + 300));\r
            }\r
        }\r
    }\r
});\r
\r
VAR数组1 = ['一','B','C'];\r
VAR数组2 = ['D','E','F'];\r
VAR ARRAY3 = ['G','H','我'];\r
\r
array1.pushArrayMembers(数组2,ARRAY3);\r
\r
document.body.textContent = JSON.stringify(数组1,NULL,4);

\r

\r
\r

This question is an exact duplicate of:
How to append an array to an existing JavaScript Array?

How do you append an array to another array in JavaScript?

Other ways that a person might word this question:

  • Add an array to another
  • Concat / Concatenate arrays
  • Extend an array with another array
  • Put the contents of one array into another array

I spent some time looking for the answer to this question. Sometimes the simplest ones like these are the hardest to find answers to, so I am adding the question here hopefully with plenty of key words and phrases as per this blog post. Please feel free to answer this question with any other helpful information or edit the key words and phrases below.

解决方案

If you want to modify the original array instead of returning a new array, use .push()...

array1.push.apply(array1, array2);
array1.push.apply(array1, array3);

I used .apply to push the individual members of arrays 2 and 3 at once.

or...

array1.push.apply(array1, array2.concat(array3));


To deal with large arrays, you can do this in batches.

for (var n = 0, to_add = array2.concat(array3); n < to_add.length; n+=300) {
    array1.push.apply(array1, to_add.slice(n, n+300));
}


If you do this a lot, create a method or function to handle it.

var push_apply = Function.apply.bind([].push);
var slice_call = Function.call.bind([].slice);

Object.defineProperty(Array.prototype, "pushArrayMembers", {
    value: function() {
        for (var i = 0; i < arguments.length; i++) {
            var to_add = arguments[i];
            for (var n = 0; n < to_add.length; n+=300) {
                push_apply(this, slice_call(to_add, n, n+300));
            }
        }
    }
});

and use it like this:

array1.pushArrayMembers(array2, array3);

var push_apply = Function.apply.bind([].push);
var slice_call = Function.call.bind([].slice);

Object.defineProperty(Array.prototype, "pushArrayMembers", {
    value: function() {
        for (var i = 0; i < arguments.length; i++) {
            var to_add = arguments[i];
            for (var n = 0; n < to_add.length; n+=300) {
                push_apply(this, slice_call(to_add, n, n+300));
            }
        }
    }
});

var array1 = ['a','b','c'];
var array2 = ['d','e','f'];
var array3 = ['g','h','i'];

array1.pushArrayMembers(array2, array3);

document.body.textContent = JSON.stringify(array1, null, 4);

这篇关于追加数组在JavaScript中另一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆