Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

How can I remove a specific item from an array?

I have an array of numbers and I'm utilizing the .push() strategy to add elements to it.

Is there a straightforward method to eliminate a particular element from an array?

I'm searching for what could be compared to something like:
array.remove(number);

I need to utilize core JavaScript. Frameworks are not permitted.
by

2 Answers

MounikaDasa
don't know how you are expecting array.remove(int) to behave. There are three possibilities I can think of that you might want.

To remove an element of an array at an index i:

array.splice(i, 1);
If you want to remove every element with value number from the array:

for (var i = array.length - 1; i >= 0; i--) {
if (array[i] === number) {
array.splice(i, 1);
}
}
If you just want to make the element at index i no longer exist, but you don't want the indexes of the other elements to change:

delete array[i];
sandhya6gczb
You can use filter method to remove item from array in javascript.
For example:

let array= [5,10,12,3,4];

let value=12;
array = array.filter(element => element !== value);
console.log(array)

Login / Signup to Answer the Question.