/

Removing items from arrays

javascript
On this page
  • Remove single item from array
  • Remove multiple items from array

Remove single item from array

js
const removeItemFromArray = (arr, value) => {
var index = arr.indexOf(value)
if (index > -1) {
arr.splice(index, 1)
}
return arr
}
// Usage
console.log(removeItemFromArray([2, 5, 9, 1, 5, 8, 5], 5))

Remove multiple items from array

js
const removeAllItemsFromArray = (arr, value) => {
var i = 0
while (i < arr.length) {
if (arr[i] === value) {
arr.splice(i, 1)
} else {
++i
}
}
return arr
}
// Usage
console.log(removeAllItemsFromArray([2, 5, 9, 1, 5, 8, 5], 5))
Edit this page
Want to make your own site like this?
Try gatsby-theme-code-notes by Zander Martineau.
Notes on code. My second brain, by Zander Martineau.