For example:
var array1 = ["one", "two", "three"];
var array2 = ["1", "2", "3"];
How to turn these arrays to var array = [["one", "1"], ["two", "2"], ["three", "3"]]
?
For example:
var array1 = ["one", "two", "three"];
var array2 = ["1", "2", "3"];
How to turn these arrays to var array = [["one", "1"], ["two", "2"], ["three", "3"]]
?
var array1 = ["one","two","three"],
array2 = ["1","2","3"],
newarray = array1.map(function(c, i) { return [ c, array2[i] ] });
alert(JSON.stringify(newarray));
This assumes the length of both arrays are always equal.
You can use underscore with the zip function:
var array1 = ["one", "two", "three"];
var array2 = ["1", "2", "3"];
_.zip(array1, array2);
// [["one", "1"], ["two", "2"], ["three", "3"]]
zip
Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. Use with apply to pass in an array of arrays. If you're working with a matrix of nested arrays, this can be used to transpose the matrix.
_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]); => [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]