为什么所有的数组都在此集合中删除一个元素而不是被引用的元素?

发布于 2025-01-24 16:42:34 字数 1129 浏览 4 评论 0 原文

我的代码:

uniformPieces3 = ['company1_hat', 'company1_glasses', 'company2_hat']
uniformSet3 = ['hat', 'glasses']


f1 = function(uniformSet, uniformPieces) {

uniformPieces.sort();
let arr1 = []
for (i = 0; i < uniformPieces.length; i++){
    arr1[i] = uniformPieces[i].substr(0, uniformPieces[i].indexOf('_'))
}
let hold = new Set(arr1);
let missingPieces = new Set
hold.forEach(element => missingPieces[element] = uniformSet);



uniformPieces.forEach(e=>{
  let t = e.split('_');
  if(missingPieces.hasOwnProperty(t[0])){
    var index = missingPieces[t[0]].indexOf(t[1]);

    if (index !== -1) {
      console.log(t[0])
      missingPieces[t[0]].splice(index, 1);
     
    }

  }
  });

  console.log(missingPieces)

}

f1(uniformSet3, uniformPieces3);

当我在错误点上调用 splice()时,它在每个数组中删除一个元素,而不是集合中的数组。例如:

Set(0) {
  company1: [ 'hat', 'glasses' ],
  company2: [ 'hat', 'glasses' ]}

然后缺失[t [0]]。剪接(index,1)被调用,它应该删除一个元素,而是产生:

Set(0) { company1: [ 'hat' ], company2: [ 'hat' ] }

从集合中的所有数组中删除该元素。

My code:

uniformPieces3 = ['company1_hat', 'company1_glasses', 'company2_hat']
uniformSet3 = ['hat', 'glasses']


f1 = function(uniformSet, uniformPieces) {

uniformPieces.sort();
let arr1 = []
for (i = 0; i < uniformPieces.length; i++){
    arr1[i] = uniformPieces[i].substr(0, uniformPieces[i].indexOf('_'))
}
let hold = new Set(arr1);
let missingPieces = new Set
hold.forEach(element => missingPieces[element] = uniformSet);



uniformPieces.forEach(e=>{
  let t = e.split('_');
  if(missingPieces.hasOwnProperty(t[0])){
    var index = missingPieces[t[0]].indexOf(t[1]);

    if (index !== -1) {
      console.log(t[0])
      missingPieces[t[0]].splice(index, 1);
     
    }

  }
  });

  console.log(missingPieces)

}

f1(uniformSet3, uniformPieces3);

When I call splice() on misingPieces, it is deleting an element in every array rather than the specific the array within the set. For example:

Set(0) {
  company1: [ 'hat', 'glasses' ],
  company2: [ 'hat', 'glasses' ]}

Then missingPieces[t[0]].splice(index, 1) is called, which should delete one element, but instead yields:

Set(0) { company1: [ 'hat' ], company2: [ 'hat' ] }

Thus deleting the element from all arrays in the set.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

烟酉 2025-01-31 16:42:35

我认为在快速阅读时,您正在存储相同的参考( simerferet ),因此,当您拼接一个引用时,它会拼接全部(因为它们实际上是相同的引用)

尝试复制 code> solifterset 而不是直接分配

  hold.forEach(element => missingPieces[element] = [...uniformSet]);

(我没有测试)

I think at a quick read over, you are storing the same reference (uniformSet) and so when you splice one, it splices all (because they are all actually the same referrence)

try copying uniformSet instead of assigning it directly

  hold.forEach(element => missingPieces[element] = [...uniformSet]);

(I didn't test this)

樱&纷飞 2025-01-31 16:42:35

非主要数据类型为通过参考通过。如果您想创建一个副本而不是为同一参考创建另一个变量,则需要克隆/复制所有值。如果您在此处使用第二行,则 my_object enose_variable_for_same_reference 请参阅同一对象。

let my_object = {};
let another_variable_for_same_reference = my_object;
let object_with_copied_values = {...my_object};

Non-primitive data types are passed by reference. If you would like to create a copy instead of creating another variable for the same reference, you need to clone/copy all values. If you use the second line here, my_object and another_variable_for_same_reference refer to the same object.

let my_object = {};
let another_variable_for_same_reference = my_object;
let object_with_copied_values = {...my_object};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文