remove element from array javascript

JavaScript
// Remove single item
function removeItemOnce(arr, value) {
   var index = arr.indexOf(value);
   if (index > -1) {
      arr.splice(index, 1);
   }
   return arr;
}

// Remove all items
function removeItemAll(arr, value) {
   var i = 0;
   while (i < arr.length) {
      if (arr[i] === value) {
         arr.splice(i, 1);
      } else {
         ++i;
      }
   }
   return arr;
}

// Usage
console.log(removeItemOnce([2, 5, 9, 1, 5, 8, 5], 5));
console.log(removeItemAll([2, 5, 9, 1, 5, 8, 5], 5));var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car");//get  "car" index
//remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red","blue","green"]let numbers = [1,2,3,4]

// to remove last element
let last_num = numbers.pop();
// numbers will now be [1,2,3]

// to remove first element
let first_num = numbers.shift();
//numbers will now be [2,3]

// to remove at an index
let number_index = 1
let index_num = numbers.splice(number_index,1); //removes 1 element at index 1
//numbers will now be [2]

//these methods will modify the numbers array and return the removed elementlet fruit = ['apple', 'banana', 'orange', 'lettuce']; 
// ^^ An example array that needs to have one item removed

fruit.splice(3, 1); // Removes an item in the array using splice() method
// First argument is the index of removal
// Second argument is the amount of items to remove from that index and on

//using filter() method => it returns a new array
data = [1, 2, 3, 4, 5, 6];
let filteredData = data.filter((i) => {
    return i > 2
});
console.log(filteredData) // [3, 4, 5, 6]let numbers = [1,2,3,4]

// to remove last element
let lastElem = numbers.pop()

// to remove first element
let firstElem = numbers.shift()

// both these methods modify array while returning the removed elementlet values = [1,2,3,4,5,7,8,9,10]

// only keep n values from an array
values.length = 5
Source

Also in JavaScript: