如何将两个数组连接到一个数组中?(复制)

时间:2021-07-21 09:05:40

This question already has an answer here:

这个问题已经有了答案:

I'm trying to combine 2 arrays in javascript into one.

我试着把两个数组结合成一个。

var lines = new Array("a","b","c");
lines = new Array("d","e","f");

This is a quick example, i want to be able to combine them so that when the second line is read the 4th element in the array would return "d"

这是一个简单的例子,我希望能够合并它们这样当第二行读取时数组中的第4个元素将返回d

How would i do this?

我该怎么做呢?

1 个解决方案

#1


248  

var a = ['a','b','c'];
var b = ['d','e','f'];
var c = a.concat(b); //c is now an an array with: ['a','b','c','d','e','f']
console.log( c[3] ); //c[3] will be 'd'

#1


248  

var a = ['a','b','c'];
var b = ['d','e','f'];
var c = a.concat(b); //c is now an an array with: ['a','b','c','d','e','f']
console.log( c[3] ); //c[3] will be 'd'