Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove a specific value from an array in JavaScript?

Suppose, i have an array

const anArray = ['value 1', 'value 2', 'value 3', 'value 4', 'value 5'];

If i want to remove the value 3 from anArray but don't know the position of that value in the array, how can i remove that?

Note: I'm a beginner in JavaScript


1 Answers

Use indexOf to get the index, and splice to delete:

const anArray = ['value 1', 'value 2', 'value 3', 'value 4', 'value 5'];
anArray.splice(anArray.indexOf("value 3"), 1);
console.log(anArray);
.as-console-wrapper { max-height: 100% !important; top: auto; }
like image 123
Jack Bashford Avatar answered Oct 20 '25 13:10

Jack Bashford