Imagine que tenemos una matriz con 3 matrices dentro y cada elemento es una matriz de 7 elementos. (como arr[3][7]) Quiero transferir estos elementos a una nueva matriz como arr2[3][1][7]. Probé el método foreach y copia la última matriz anidada en todos los elementos nuevos.
var c3 = new Array(players).fill(new Array(1).fill(new Array(7))) // console.log(c2) // c2= [ // [x1, x2, x3, x4, x5, x6, x7], // [y1, y2, y3, y4, y5, y6, y7], // [z1, z2, z3, z4, z5, z6, z7]] c2.forEach(function (val, ind) { c3[ind][0] = c2[ind] }) // it supposed to return c3: // [ // [[x1, x2, x3, x4, x5, x6, x7]], // [[y1, y2, y3, y4, y5, y6, y7]], // [[z1, z2, z3, z4, z5, z6, z7]]] // But it returns: // [ // [[z1, z2, z3, z4, z5, z6, z7]], // [[z1, z2, z3, z4, z5, z6, z7]], // [[z1, z2, z3, z4, z5, z6, z7]]]Algo como esto ?
const data = [ ['x1', 'x2', 'x3', 'x4', 'x5', 'x6', 'x7'], ['y1', 'y2', 'y3', 'y4', 'y5', 'y6', 'y7'], ['z1', 'z2', 'z3', 'z4', 'z5', 'z6', 'z7'], ]; const res = data.map(v => [v]); console.log(res);espero que esto ayude
function flatten(array){ const res = []; for(let i = 0; i< array.length; i++){ if(Array.isArray(array[i])){ const flat = flatten(array[i]) for(let j = 0; j < flat.length; j++){ res.push(flat[j]) } }else{ res.push(array[i]) } } return res } console.log(flatten( [ [1], [[2],[3]], [[[[4]]]]] )) output // [1, 2, 3, 4]