简体   繁体   English

使用for循环从数组中拼接多个元素

[英]splice multiple elements from an array using for loop

I want to splice some multiple index of array here is my code example : 我想拼接数组的多个索引,这是我的代码示例:

  let arrayA = ["aa","bb","cc","dd","ee","ff","gg","hh"]; let arrrayIndexSplice = [0,1,3]; let test = null; for (let i of arrrayIndexSplice) { test = arrayA.splice(i,1); } console.log(arrayA); 

expected result is ["cc","ee","ff","gg","hh"]; 预期结果为[“ cc”,“ ee”,“ ff”,“ gg”,“ hh”];

actual result is ["bb", "dd", "ee", "gg", "hh"] 实际结果是[“ bb”,“ dd”,“ ee”,“ gg”,“ hh”]

When splicing out the index of all following values gets decreased. 拼接时,所有后续值的索引都会减少。 Therefore splice out the last first: 因此,拼接出最后一个:

let arrrayIndexSplice = [0, 1, 3];
for (let i of arrrayIndexSplice.sort().reverse()) {
 arrayA.splice(i, 1);
}

Or just filter: 或者只是过滤器:

 arrayA = arrayA.filter((_, i) => !arrayIndexSplice.includes(i));

you should start from ending means i-- let's try following 您应该从结尾开始,我-让我们尝试跟随

let arrayA=["aa","bb","cc","dd","ee","ff","gg","hh"];
let arrrayIndexSplice=[0,1,3];
for (let i=arrrayIndexSplice.length-1;i>=0;i--){arrayA.splice(arrrayIndexSplice[i],1)}

The problem may be that you are traversing the array in ascending order. 问题可能是您以升序遍历数组。 So when you splice the first element, index of other elements change. 因此,当您拼接第一个元素时,其他元素的索引也会改变。 Negative iteration on array may help. 数组上的负迭代可能会有所帮助。

let arrayA=["aa","bb","cc","dd","ee","ff","gg","hh"];
let arrrayIndexSplice=[0,1,3];
for (let i=arrrayIndexSplice.length-1; i > -1; i--){arrayA.splice(i,1)}

You can filter out all the elements with indexes you want to exclude. 您可以过滤出所有要排除索引的元素。

 let arrayA = ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh"]; let arrrayIndexSplice = [0, 1, 3]; arrayA = arrayA.filter((item, index)=> !arrrayIndexSplice.includes(index)); console.log(arrayA); 

My idea is to replace the items in that index with a special character or string and then remove that special character or string using replace again: 我的想法是用特殊字符或字符串替换该索引中的项目,然后再次使用replace删除该特殊字符或字符串:

 let arrayA = ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh"]; let arrrayIndexSplice = [0, 1, 3]; let test = null; arrayA.forEach((val, index) => { if(arrrayIndexSplice.includes(index)){ arrayA[index] = '##'; } }); arrayA = arrayA.join(',').replace(/##,/g, '').split(','); console.log(arrayA); 

While you traverse through the for of loop you are constantly changing the array itself and so the indices are shifted to the right every loop step 在遍历for of循环时,您会不断更改数组本身,因此索引会在每个循环步移到右侧

Your example 你的例子

arrayA: ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh"]
// start loop
// 1st iteration
// test = arrayA.splice(i,1);
test = 0
arrayA = ["bb", "cc", "dd", "ee", "ff", "gg", "hh"]  // index 0 removed

// 2nd iteration
// test = arrayA.splice(i,1);
test = 1
arrayA = ["bb", "dd", "ee", "ff", "gg", "hh"]  // index 1 removed

// 3rd iteration
// test = arrayA.splice(i,1);
test = 3
arrayA = ["bb", "dd", "ee", "gg", "hh"]  // index 3 removed

This works: You need to get the index of the current arrrayIndexSplice element, so you can decrease the splice start index by that amount 这有效:您需要获取当前arrrayIndexSplice元素的索引,因此可以将拼接起始索引减少该数量

 console.clear() let arrayA = ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh"]; let arrrayIndexSplice = [0, 1, 3]; for (let k in arrrayIndexSplice) { if (arrrayIndexSplice.hasOwnProperty(k)) { arrayA.splice(arrrayIndexSplice[k]-k, 1) } } console.log(arrayA) 

This also works: It doesn't work with a loop, but a filter function 这也起作用:它不适用于循环,但可以使用filter功能

 let arr = ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh"]; let indices = [0,1,3] function filterIndices(arr, indices, swap = false) { return arr.filter((item, index, source) => { return swap ? !indices.includes(index) : indices.includes(index); }) } newArr = filterIndices(arr, indices, true) console.log(newArr) 

You can use below code : 您可以使用以下代码:

let arrayA = ["aa", "bb", "cc", "dd", "ee", "ff", "gg", "hh"];
let arrrayIndexSplice = [0, 1, 3];
for (let i of arrrayIndexSplice.length) {
  arrayA.splice(arrrayIndexSplice[i], 1)
}

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

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