Tengo 4 matrices diferentes

arr1 = ["a", "b", "c", "d"];
arr2 = ["1", "2", "3", "4"];
arr3 = ["a1", "b2", "c3", "d4"];
arr4 = ["aa", "bb", "cc", "dd"];

Necesito crear una nueva matriz de la siguiente manera:

new_array = ["a", "1", "a1", "aa"];

No quise abordar solo el primer valor, lo que quise decir es crear una nueva matriz con todos los valores ... perdón si los confundí a todos ...

Solución

var arr1 = ["a", "b", "c", "d"];
var arr2 = ["1", "2", "3", "4"];
var arr3 = ["a1", "b2", "c3", "d4"];
var arr4 = ["aa", "bb", "cc", "dd"];
var new_array =[];
i=0;
$.each(arr1, function(i, item){
    new_array.push([arr1[i], arr2[i], arr3[i], arr4[i]]);
    i++;
 });

console.log(new_array);
0
Nadim 14 ene. 2017 a las 02:29

3 respuestas

La mejor respuesta
new_array = [ arr1[0],arr2[0],arr3[0],arr4[0] ]
2
rupps 13 ene. 2017 a las 23:32

Puedes usar map:

var arr1 = ["a", "b", "c", "d"];
var arr2 = ["1", "2", "3", "4"];
var arr3 = ["a1", "b2", "c3", "d4"];
var arr4 = ["aa", "bb", "cc", "dd"];

var result = [arr1, arr2, arr3, arr4].map(a => a[0]);

console.log(result);
2
trincot 13 ene. 2017 a las 23:36

Uno más, solo por la diversidad ... :)

var arr1 = ["a", "b", "c", "d"];
var arr2 = ["1", "2", "3", "4"];
var arr3 = ["a1", "b2", "c3", "d4"];
var arr4 = ["aa", "bb", "cc", "dd"];

var new_arr = arr1.concat(arr2,arr3,arr4).filter(function(val,i) {
return i%4==0;
});

console.log(new_arr);
0
sinisake 14 ene. 2017 a las 00:04