showing results for - "javascript function to add or remove from array if"
Antoine
10 Oct 2019
1function removeItemOnce(arr, value) {
2  var index = arr.indexOf(value);
3  if (index > -1) {
4    arr.splice(index, 1);
5  }
6  return arr;
7}
8
9function removeItemAll(arr, value) {
10  var i = 0;
11  while (i < arr.length) {
12    if (arr[i] === value) {
13      arr.splice(i, 1);
14    } else {
15      ++i;
16    }
17  }
18  return arr;
19}
20//Usage
21console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
22console.log(removeItemAll([2,5,9,1,5,8,5], 5))
queries leading to this page
array add and remove jshow to add and remove elements in array in javascript by mdnadd element on array javascript and removejavascript push and remove from arrayjs add and remove from array how to add or delete elements from javascript array 3fadd and remove javascipt arrayjs array if available remove or addhow to remove or add item in array nodeadd and remove item to array javascriptadding and remove elements in array in javascriptjs add or remove element from arrayhow to add and remove value from array in javascriptremove array element and add elementshow to add and remove elements in an array jsadd and remove elements in array javascriptclick to remove existing item from array and lick to add javascriptarray push and removeremove or add from array javascriptadd and remove array of item in javascriptarray add and remove javascriptjavascript array add and remove elementshow to add and remove data in array in javascriptadd to array or remove from arrayjs add and remove from a arrayjavascript add or remove from arrayadd or remove from array javascriptnode js add and remove from arrayadd and remove from array javascriptjavascript array add and removejavascript to add and delete items from arraypush and remove array javascriptarray push and remove javascriptarray add and deleteadd or remove item from array jshow can remove and add element to array in jsadd remove value in array javascriptjs array append and removejavascript function to add or remove from array ifcan we add or remove an element in an arrayremove and adding in an array javascriptadd or remove items in javascript arrayjs add to array removecheck if item is array javascript add or removeadd and remove element from array javascriptadd and remove item from array javascriptadd and remove items from array javascriptjavascript add and remove from arraydelete insert in js array how to add and remove in javascript arrayjs array add and removeadd or remove elements from array javascriptjavascript add to array remove from arrayhow to do if array element remove and addjavascript array where you can add and remove valuehow to add and remove an element from an array 3fremove or add array itemto add or remove into an array javascripthow to add and remove array value in javascripthow to add and remove items from an array in jsremove or add item to arrayremove and add from an array javascriptadd delete from an array jsjs add or remove from arrayadd or remove array in javascripthow to add and remove elements in an arrayhow to add and remove elements in array in javascripthow to delete and add new data in array using jsjs push and remove from arrayadd and remove js arraysadd remove item from array javascriptadd or remove element from array jsremove and add element from array javascriptjavascript function to add or remove from array if