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?
- 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. Usesplice
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
1 Answer
Reset to default 11That 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