14
snippets/array-pull-(mutates-array).md
Normal file
14
snippets/array-pull-(mutates-array).md
Normal file
@ -0,0 +1,14 @@
|
||||
### Array pull (mutates array)
|
||||
|
||||
Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
|
||||
Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
|
||||
|
||||
```js
|
||||
const pull = (arr, ...args) => {
|
||||
let pulled = arr.filter((v, i) => args.includes(v));
|
||||
arr.length = 0; pulled.forEach(v => arr.push(v));
|
||||
};
|
||||
// let myArray = ['a', 'b', 'c', 'a', 'b', 'c'];
|
||||
// pull(myArray, 'a', 'c');
|
||||
// console.log(myArray) -> [ 'b', 'b' ]
|
||||
```
|
||||
@ -56,7 +56,7 @@ is-function:utility
|
||||
is-number:utility
|
||||
is-string:utility
|
||||
is-symbol:utility
|
||||
JSON-to-date:date
|
||||
json-to-date:
|
||||
last-of-list:array
|
||||
log-function-name:function
|
||||
measure-time-taken-by-function:utility
|
||||
@ -71,6 +71,7 @@ pick:array
|
||||
pipe-functions:function
|
||||
powerset:math
|
||||
promisify:function
|
||||
pull:array
|
||||
random-integer-in-range:math
|
||||
random-number-in-range:math
|
||||
read-file-as-array-of-lines:node
|
||||
@ -98,4 +99,4 @@ URL-parameters:utility
|
||||
UUID-generator:utility
|
||||
validate-email:utility
|
||||
validate-number:utility
|
||||
write-JSON-to-file:node
|
||||
write-json-to-file:
|
||||
|
||||
Reference in New Issue
Block a user