Tuesday, April 24, 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

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...