diginext-utils
Version:
README.md
30 lines • 874 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeItem = removeItem;
/**
* Removes the first occurrence of an item from an array.
* Returns a new array without modifying the original.
*
* @template T - The type of elements in the array
* @param array - The array to remove from
* @param item - The item to remove
* @returns A new array with the item removed
*
* @example
* ```ts
* removeItem([1, 2, 3, 2], 2); // [1, 3, 2]
* removeItem(['a', 'b', 'c'], 'b'); // ['a', 'c']
* removeItem([1, 2, 3], 4); // [1, 2, 3]
* ```
*/
function removeItem(array, item) {
if (!Array.isArray(array)) {
return [];
}
const index = array.indexOf(item);
if (index === -1) {
return [...array];
}
return [...array.slice(0, index), ...array.slice(index + 1)];
}
//# sourceMappingURL=removeItem.js.map