Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I have an array of numbers, and I'm using the .push() method to add elements to it.

Is there a simple way to remove a specific element from an array? The equivalent of something like array.remove(number);.

I have to use core JavaScript - no frameworks are allowed.

1 Answer

0 votes
by (106k points)
edited by

We have splice method in JavaScript which finds the index of the array element that you want to remove.

In splice() method we pass three attributes the first is the index of the element that you want to remove the second is the second attribute is the number of elements that you want to remove and the third attribute is if you want to replace that value with some other value. Below is the example for the same:-

var array = [3, 2, 3];

var index = array.indexOf(2);

if (index > -1) {

array.splice(index, 1);

console.log(array);

image

Related questions

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...