0
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?

7
  • can you plese provide an example of console.log(secondSpecsArr); Commented May 30, 2017 at 10:09
  • 30 slice operation30 <strong>DVD+R</strong> : 16X<br> 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." Commented May 30, 2017 at 10:10
  • slice did not change your array. Use splice instead. Commented May 30, 2017 at 10:11
  • secondSpecsArr = secondSpecsArr.slice(j,1) maybe you are missing this Commented May 30, 2017 at 10:11

1 Answer 1

12

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.