let linkMatrix = [ [0,0,1,0], [1,0,0,1], [1,1,0,1], [0,1,0,0] ]; let newMatrix = []; function linkToPage(){ for(let j = 0; j < linkMatrix.length; j--){ newMatrix = linkMatrix.splice(linkMatrix[j], 1); console.log(linkMatrix + " Here is linkMatrix"); for(let i = 0; i < newMatrix.length; i++){ newMatrix.splice(newMatrix[i]); console.log(newMatrix + " Here is newMatrix"); } }** Lo que estoy tratando de hacer es recorrer la primera matriz pero eliminar la primera matriz, porque no necesito recorrer eso, luego recorrer el resto de las matrices, pero el único valor que necesito es el valor en el índice de la matriz eliminada, para comprender mejor: si tuviéramos una matriz que usara algo como esto arr = [[0,1],[1,0],[1,1]] entonces elimine [0,1] y porque es arr[0], me gustaría recorrer el índice 0 de las otras matrices para obtener 1 y 1, luego volver a la matriz original eliminar arr[1] y recorrer arr[0],arr [2] al índice 1 de las matrices para obtener [1,1] **
**Yeah, so the wanted result from my link matrix would be: [0,0,1,0] = 2 [1,0,0,1] = 2 [1,1,0,1] = 1 [0,1,0,0] = 2 because there is 2 other arrys pointing to the first array, the same for the second and fourth, but there is only one array pointing to the third array **Puede agregar los valores de las columnas.
const getColSum = matrix => matrix.reduce((r, a) => a.map((v, i) => (r[i] || 0) + v), []); console.log(...getColSum([[0, 0, 1, 0], [1, 0, 0, 1], [1, 1, 0, 1], [0, 1, 0, 0]])); console.log(...getColSum([[0, 0, 0], [1, 0, 0], [1, 1, 0]]));Una versión sin (casi) métodos de matriz.
function getColSum (matrix) { const result = Array(matrix[0].length).fill(0); for (const row of matrix) { for (let i = 0; i < row.length; i++) result[i] += row[i]; } return result; } console.log(...getColSum([[0, 0, 1, 0], [1, 0, 0, 1], [1, 1, 0, 1], [0, 1, 0, 0]])); console.log(...getColSum([[0, 0, 0], [1, 0, 0], [1, 1, 0]]));