❌
Removing items from arrays
Remove single item from array
js
const removeItemFromArray = (arr, value) => {var index = arr.indexOf(value)if (index > -1) {arr.splice(index, 1)}return arr}// Usageconsole.log(removeItemFromArray([2, 5, 9, 1, 5, 8, 5], 5))
Remove multiple items from array
js
const removeAllItemsFromArray = (arr, value) => {var i = 0while (i < arr.length) {if (arr[i] === value) {arr.splice(i, 1)} else {++i}}return arr}// Usageconsole.log(removeAllItemsFromArray([2, 5, 9, 1, 5, 8, 5], 5))