慕丝7291255
您可以访问所有值并检查索引是否为零或实际数组的长度减一。const array = [[3, 8, 9, 7, 6], [6, 3, 8, 9, 7], [7, 6, 3, 8, 9]], perimeter = array.reduce((sum, row, i, a) => row.reduce((s, v, j, b) => s + (i === 0 || j === 0 || i + 1 === a.length || j + 1 === b.length ? v : 0, sum) ), 0 );console.log(perimeter);
慕运维8079593
此函数将返回矩阵中的所有周长数字,然后对每个项目求平并求和。const arr = [[3, 8, 9, 7, 6], [6, 3, 8, 9, 7], [7, 6, 3, 8, 9]];var snail = function(arr) { let output = []; output.push(arr[0]); output.push(arr[arr.length - 1]); for(let item of arr){ if(item != arr[0] && item != arr[arr.length - 1]){ output.push(item[0]); output.push(item[item.length - 1]); } }return output}const perimeter = snail(arr).flat(1);const sum = perimeter.reduce((a, b) => a + b);console.log(sum);