813 B
813 B
title, type, language, tags, cover, dateModified
| title | type | language | tags | cover | dateModified | |
|---|---|---|---|---|---|---|
| Pull values from array | snippet | javascript |
|
salad-2 | 2020-10-22T20:24:04+03:00 |
Mutates the original array to filter out the values specified.
- Use
Array.prototype.filter()andArray.prototype.includes()to pull out the values that are not needed. - Set
Array.prototype.lengthto mutate the passed in an array by resetting its length to0. - Use
Array.prototype.push()to re-populate it with only the pulled values.
const pull = (arr, ...args) => {
let argState = Array.isArray(args[0]) ? args[0] : args;
let pulled = arr.filter(v => !argState.includes(v));
arr.length = 0;
pulled.forEach(v => arr.push(v));
};
let myArray = ['a', 'b', 'c', 'a', 'b', 'c'];
pull(myArray, 'a', 'c'); // myArray = [ 'b', 'b' ]