Wednesday, 11 May 2016

JavaScript: How to join / combine two arrays to concatenate into one array?




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"



How would i do this?


Answer



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'

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...