admin管理员组

文章数量:1357302

while (j<secondSpecsArr.length) {
    console.log(j);
    if (regStr.test(secondSpecsArr[j])){
        console.log('slice operation' + j + ' ' + secondSpecsArr[j]);
        secondSpecsArr.slice(j,1); 
    } else { j++; }
}

I am deleting elements from Array, that include string 'strong'. But its not deleting at least anything! Like,console.log('slice operation' + j + ' ' + secondSpecsArr[j]);` is working, but slice() isn'nt, and I get old array after this. Where's the problem?

while (j<secondSpecsArr.length) {
    console.log(j);
    if (regStr.test(secondSpecsArr[j])){
        console.log('slice operation' + j + ' ' + secondSpecsArr[j]);
        secondSpecsArr.slice(j,1); 
    } else { j++; }
}

I am deleting elements from Array, that include string 'strong'. But its not deleting at least anything! Like,console.log('slice operation' + j + ' ' + secondSpecsArr[j]);` is working, but slice() isn'nt, and I get old array after this. Where's the problem?

Share Improve this question asked May 30, 2017 at 10:08 andrey andreyandrey andrey 112 silver badges6 bronze badges 7
  • can you plese provide an example of console.log(secondSpecsArr); – Jurij Jazdanov Commented May 30, 2017 at 10:09
  • 30 slice operation30 <strong>DVD+R</strong> : 16X<br> – andrey andrey Commented May 30, 2017 at 10:10
  • 5 Array.prototype.slice(): "The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included). The original array will not be modified." – Andreas Commented May 30, 2017 at 10:10
  • slice did not change your array. Use splice instead. – degr Commented May 30, 2017 at 10:11
  • secondSpecsArr = secondSpecsArr.slice(j,1) maybe you are missing this – Jurij Jazdanov Commented May 30, 2017 at 10:11
 |  Show 2 more ments

1 Answer 1

Reset to default 11

That is what slice is for. It doesn't remove from the array, it extracts. splice is removing from the array.

var a = [1,2,3];

a.slice(0,1) // [1]
//a = [1,2,3]

a.splice(0,1) // [1];
// a = [2,3]

splice

slice

本文标签: JavaScript slice() is not deleting elements from arrayStack Overflow