为什么推送到新数组会给我不正确的数据列表?
why does pushing to a new array gives me incorrect list of data?
- 我希望排列数组如下所示
- 但是我在代码片段中执行代码得到的结果不是我想要的。
- 谁能解释一下,为什么输出与我预期的不一样。
permutation [[ "a","b","a"],["b","a","a"],["a","a","b"]]
function permAlone(str) {
const createPermutation = () => {
let arr = Array.from(str);
let permutation = [];
Array.from(str).forEach(_ele => {
const first = arr.shift();
arr = arr.concat(first);
permutation.push(arr)
});
console.log('permutation',permutation);
};
createPermutation();
return str;
}
permAlone("aab");
当您将 arr
推入排列时,它会保留一个引用,当您在后续迭代中修改 arr 时,您修改它会影响排列数组的内容,一种强制使用数组副本的简单方法方法切片。
function permAlone(str) {
const createPermutation = () => {
let arr = Array.from(str);
let permutation = [];
Array.from(str).forEach(_ele => {
const first = arr.shift();
arr = arr.concat(first);
permutation.push(arr.slice())
});
console.log('permutation',permutation);
};
createPermutation();
return str;
}
permAlone("aab");
澄清
为了回应您的评论,我创建了一个更简单的示例。
const arr1 = []
const arr = []
arr.push(1)
arr1.push(arr)
console.log(arr1) // here you se what you would expect
arr.push(2) // arr1 has a reference to this object
console.log(arr1) // here you see that arr1 changed
如果你推送一个切片,这将添加一个 arr 的副本,而不是对它的引用
const arr1 = []
const arr = []
arr.push(1)
arr1.push(arr.slice()) // what is added is a copy of arr
console.log(arr1) // here you se what you would expect
arr.push(2) // arr1 does not have a reference to this object
console.log(arr1) // here you see that arr1 is not changed
您的代码中的问题是您正在推送 arr 数组的深层副本。相反,您应该使用 slice() 方法进行浅拷贝。当您深度复制数组时,它会传递其地址,该地址会在最后一次迭代后更改其值,而不是您应该使用 slice 方法传递数组的值,这样您的代码才能正常工作。
function permAlone(str) {
const createPermutation = () => {
let arr = Array.from(str);
let permutation = [];
Array.from(str).forEach(_ele => {
const first = arr.shift();
arr = arr.concat(first);
permutation.push(arr.slice())
});
console.log('permutation',permutation);
};
createPermutation();
return str;
}
permAlone("aab");
- 我希望排列数组如下所示
- 但是我在代码片段中执行代码得到的结果不是我想要的。
- 谁能解释一下,为什么输出与我预期的不一样。
permutation [[ "a","b","a"],["b","a","a"],["a","a","b"]]
function permAlone(str) {
const createPermutation = () => {
let arr = Array.from(str);
let permutation = [];
Array.from(str).forEach(_ele => {
const first = arr.shift();
arr = arr.concat(first);
permutation.push(arr)
});
console.log('permutation',permutation);
};
createPermutation();
return str;
}
permAlone("aab");
当您将 arr
推入排列时,它会保留一个引用,当您在后续迭代中修改 arr 时,您修改它会影响排列数组的内容,一种强制使用数组副本的简单方法方法切片。
function permAlone(str) {
const createPermutation = () => {
let arr = Array.from(str);
let permutation = [];
Array.from(str).forEach(_ele => {
const first = arr.shift();
arr = arr.concat(first);
permutation.push(arr.slice())
});
console.log('permutation',permutation);
};
createPermutation();
return str;
}
permAlone("aab");
澄清
为了回应您的评论,我创建了一个更简单的示例。
const arr1 = []
const arr = []
arr.push(1)
arr1.push(arr)
console.log(arr1) // here you se what you would expect
arr.push(2) // arr1 has a reference to this object
console.log(arr1) // here you see that arr1 changed
如果你推送一个切片,这将添加一个 arr 的副本,而不是对它的引用
const arr1 = []
const arr = []
arr.push(1)
arr1.push(arr.slice()) // what is added is a copy of arr
console.log(arr1) // here you se what you would expect
arr.push(2) // arr1 does not have a reference to this object
console.log(arr1) // here you see that arr1 is not changed
您的代码中的问题是您正在推送 arr 数组的深层副本。相反,您应该使用 slice() 方法进行浅拷贝。当您深度复制数组时,它会传递其地址,该地址会在最后一次迭代后更改其值,而不是您应该使用 slice 方法传递数组的值,这样您的代码才能正常工作。
function permAlone(str) {
const createPermutation = () => {
let arr = Array.from(str);
let permutation = [];
Array.from(str).forEach(_ele => {
const first = arr.shift();
arr = arr.concat(first);
permutation.push(arr.slice())
});
console.log('permutation',permutation);
};
createPermutation();
return str;
}
permAlone("aab");