javascript function to add or remove from array if
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;
}
//Usage
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,5], 5))
Also in JavaScript:
- javascript array.find
- angular import service
- mongoose find by and delete
- javascript training
- save or update mongoose
- google login with react native
- how to convert an array into an object using javascript
- Setting axios base url dynamically
- react not recognizing lorem
- javascript format date
- how to english paragraph matching in javascript
- jquery style display
- error build react Failed to execute 'replaceState' on 'History': A history state object with URL 'file:///' cannot be created in a document with origin 'null' and URL
- loop javascript
- react record voice
- import path nodejs
- find object in array javascript with property
- jstree get data
- python save list to json
- js get number from string
- for in javascript
- reverse keys and values in object javascript
- javascript is null
- install nuxt js