es-toolkit
Version:
A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.
21 lines (19 loc) • 522 B
JavaScript
function remove(arr, shouldRemoveElement) {
const originalArr = arr.slice();
const removed = [];
let resultIndex = 0;
for (let i = 0; i < arr.length; i++) {
if (shouldRemoveElement(arr[i], i, originalArr)) {
removed.push(arr[i]);
continue;
}
if (!Object.hasOwn(arr, i)) {
delete arr[resultIndex++];
continue;
}
arr[resultIndex++] = arr[i];
}
arr.length = resultIndex;
return removed;
}
export { remove };