In this tutorial, we will explain to you how to remove specific element from array in javascript(JavaScript Remove specific element from an array). there are many ways to remove specific element from an array in Javascript. PHP provides array_unique() method for remove duplicate value in array. so you can see our following example.
Javascript remove a specific element from an array using splice Method
In this example, we are taking a simple array as an example and the indexOf() and splice() method helps to remove specific element values in an array. so you can see the below example.
<script type="text/JavaScript"> var arr = [1, 2, 2, 3, 4]; var index = arr.indexOf(3); if (index > -1) { arr.splice(index, 1); } console.log(arr); // array = [1, 2, 2, 4] </script>
javascript remove a specific element from an array using filter
In this example, we are taking an string simple array as an example and the filter() method helps to remove the specific element in an array. here in this example, we want to remove the car value from an array. so you can see the below example.
<script type="text/JavaScript"> var arr = ['apple', 'bike', 'car']; var final_arr = ary.filter(function(e) { return e !== 'car' }); console.log(filteredAry); //output Array ["apple", "bike"] </script>
javascript remove a specific element from an array using a forEach loop
In this example, we are taking a simple array as an example and the forEach loop helps to remove the specific element in an array. so you can see the below example.
var arr = ['kuldip', 'ajay', 'dev', 'jay', 'deep']; arr.forEach(function(item, index, object) { if (item === 'jay') { object.splice(index, 1); } }); console.log(arr); //Output Array ["kuldip", "ajay", "dev", "deep"]