JavaScript:如何加入/组合两个数组以连接成一个数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3975170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 06:57:31 来源:igfitidea点击:
JavaScript: How to join / combine two arrays to concatenate into one array?
提问by Diesal11
I'm trying to combine 2 arrays in javascript into one.
我正在尝试将 javascript 中的 2 个数组合并为一个。
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?
我该怎么做?
回答by Moin Zaman
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'