简体   繁体   English

使用 javascript 中的 slice 方法存储数组的实例

[英]Storing instances of an array using the slice method in javascript

I have an array list I loop over and modify it each time.我有一个数组列表,我每次循环并修改它。 I want to store all instances of my list array in an other array I named allLists , and to do so, I'm using the slice method.我想将列表数组的所有实例存储在另一个名为allLists的数组中,为此,我使用了 slice 方法。

It seems to work in the simple example below:它似乎在下面的简单示例中起作用:

let list=[1,2,3,4];
let allList = [];

allList.push(list.slice());

list[2]=6;
allList.push(list.slice());

console.log(allList);// returns [1,2,3,4] [1,2,6,4]

But it doesn't work in the following code.但它在以下代码中不起作用。 Instead, allLists is filled with the last instance of the list array.相反, allLists填充了列表数组的最后一个实例。

 let list = Array.from({ length: 9 }, () => Array.from({ length: 9 }, () => [1, 2, 3, 4, 5, 6, 7, 8, 9])); let allLists = [list.slice()]; let indexList = []; let lengthList = []; let key = true; function handleNewIndex(list) { let newIndex = [0, 0]; let maxLength = 9; for (let i = 0; i < list.length; i++) { for (let j = 0; j < list.length; j++) { if (list[i][j].length < maxLength && list[i][j].length > 0) { maxLength = list[i][j].length; newIndex = [i, j]; } } } return newIndex; } function isSudokuValid(list) { for (let i = 0; i < list.length; i++) { for (let j = 0; j < list.length; j++) { if (list[i][j].length === 0) { return false; } } } return true; } function handleWrongSudoku(allLists, indexList, lengthList) { let counter = 1; while (lengthList[lengthList.length - counter] <= 1) { counter = counter + 1; allLists.pop(); indexList.pop(); } let wrongList = allLists.pop(); list = allLists.pop(); indexLine = indexList[indexList.length - 1][0]; indexColumn = indexList[indexList.length - 1][1]; let wrongNumber = wrongList[indexLine][indexColumn]; for (let i = 0; i < list[indexLine][indexColumn].length; i++) { if (list[indexLine][indexColumn][i] != wrongNumber) { list[indexLine][indexColumn] = list[indexLine][indexColumn][i]; } } allLists.push(list.slice()); indexLine = handleNewIndex(list)[0]; indexColumn = handleNewIndex(list)[1]; } function generateSudoku() { let indexLine = Math.floor(Math.random() * 9); let indexColumn = Math.floor(Math.random() * 9); let counter = 0; while (counter < 81) { indexList.push([indexLine, indexColumn]); let bigSquareIndex = 3 * Math.floor(indexLine / 3) + Math.floor(indexColumn / 3); lengthList.push(list[indexLine][indexColumn].length); list[indexLine][indexColumn] = list[indexLine][indexColumn][Math.floor(Math.random() * list[indexLine][indexColumn].length)]; counter = counter + 1; for (let i = 0; i < 9; i++) { for (let j = 0; j < 9; j++) { if (3 * Math.floor(i / 3) + Math.floor(j / 3) === bigSquareIndex) { let k = 0; let n = list[i][j].length; while (list[i][j][k] != list[indexLine][indexColumn] && k < n) { k = k + 1; } if (k < n) { list[i][j].splice(k, 1); } } else if (i === indexLine || j === indexColumn) { let k = 0; let n = list[i][j].length; while (list[i][j][k] != list[indexLine][indexColumn] && k < n) { k = k + 1; } if (k < n) { list[i][j].splice(k, 1); } } } } allLists.push(list.slice()); key = isSudokuValid(list); if (key === false) { //ignore this scenario, not done yet, assume key = true at all time console.log(key, lengthList, indexList, allLists); handleWrongSudoku(allLists, indexList, lengthList); key = true; //return; } else { indexLine = handleNewIndex(list)[0]; indexColumn = handleNewIndex(list)[1]; } } } generateSudoku(); console.log(allLists); // returns 81 times the same 9x9 array instead of the 81 different instances of the list array

I don't know what I'm doing wrong here.我不知道我在这里做错了什么。

Thanks for the hint Code Maniac.感谢 Code Maniac 的提示。

I used the JSON method instead to create a deep copy and it's working fine now.我改用 JSON 方法来创建深层副本,现在它工作正常。

allLists.push(JSON.parse(JSON.stringify(list)));

This post explains the difference between shallow and deep copy:这篇文章解释了浅拷贝和深拷贝的区别:

https://www.freecodecamp.org/news/how-to-clone-an-array-in-javascript-1d3183468f6a/ https://www.freecodecamp.org/news/how-to-clone-an-array-in-javascript-1d3183468f6a/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM