how to remove element of array code example
Example 1: remove element from array javascript
function removeItemOnce(arr, value) {
var index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
function removeItemAll(arr, value) {
var i = 0;
while (i < arr.length) {
if (arr[i] === value) {
arr.splice(i, 1);
} else {
++i;
}
}
return arr;
}
console.log(removeItemOnce([2, 5, 9, 1, 5, 8, 5], 5));
console.log(removeItemAll([2, 5, 9, 1, 5, 8, 5], 5));
Example 2: remove element from array javascript
data = [1, 2, 3, 4, 5, 6];
let filteredData = data.filter((i) => {
return i > 2
});
console.log(filteredData)