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");
}
}
**我想要做的是回圈遍歷第一個陣列但洗掉第一個陣列,因為我不需要回圈遍歷那個陣列,然后回圈遍歷其余的陣列,但我需要的唯一值是值在已洗掉陣列的索引中,以便更好地理解:如果我們有一個陣列,它穿著這樣的東西 arr = [[0,1],[1,0],[1,1]] 然后洗掉 [0,1] 和因為它是 arr[0],我想回圈遍歷其他陣列的 0 索引,所以我會得到 1 和 1,然后回傳原始陣列洗掉 arr[1] 并回圈遍歷 arr[0],arr [2] 到陣列的 1 索引所以我會得到 [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
**
uj5u.com熱心網友回復:
您可以添加列的值。
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]]));
沒有(幾乎)陣列方法的版本。
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]]));
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/369264.html
標籤:javascript 数组 多维数组 网页排名
