3 回答

TA貢獻(xiàn)1846條經(jīng)驗(yàn) 獲得超7個(gè)贊
這是一個(gè)經(jīng)典的遞歸算法,因?yàn)槊總€(gè)步驟都包含相同的算法:
從索引中彈出第一個(gè)索引。
繼續(xù)使用新彈出的索引指向的數(shù)組。
直到你到達(dá)最后一個(gè)元素indices
- 然后替換最低級(jí)別數(shù)組中的相關(guān)元素。
function getUpdatedArray(inputArray, indices, valueToReplace) {
const ans = [...inputArray];
const nextIndices = [...indices];
const currIndex = nextIndices.shift();
let newValue = valueToReplace;
if (nextIndices.length > 0) {
newValue = getUpdatedArray(
inputArray[currIndex],
nextIndices,
valueToReplace,
);
} else if (Array.isArray(inputArray[currIndex])) {
throw new Error('Indices array points an array');
}
ans.splice(currIndex, 1, newValue);
return ans;
}
const arr = [
[
[8, 5, 8],
[9, 9, 9],
[0, 0, 1]
],
[
[7, 8, 2],
[9, 8, 3],
[9, 5, 6]
]
];
const indices = [1, 2, 0];
const newArr = getUpdatedArray(arr, indices, 100)
console.log(newArr);

TA貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超7個(gè)贊
我認(rèn)為您正在尋找的是:
arr[index[0]][index[1]][index[2]] = value;
我無(wú)法理解您在示例的第二部分中嘗試做什么。
添加回答
舉報(bào)