0 votes
1 view
in Web Technology by (47.7k 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 (107k 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

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...