Tuesday, 24 April 2018

javascript - How to remove the matching element of the array




I have one array in JavaScript:



['html', 'css', 'perl', 'c', 'java', 'javascript']


How can I delete "perl" element?



There has to be removing the third element. It must be to remove the element with a value of "perl".



Answer



Find the index of the word, then use splice to remove it from your array.



var array = ['html', 'css', 'perl', 'c', 'java', 'javascript']  
var index = array.indexOf('perl');

if (index > -1) {
array.splice(index, 1);
}


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...