From 73f37c53c79b76aed3f50119dc694e629879d4e3 Mon Sep 17 00:00:00 2001 From: King Date: Sat, 16 Dec 2017 23:02:33 -0500 Subject: [PATCH] add pull snippet --- snippets/pull.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 snippets/pull.md diff --git a/snippets/pull.md b/snippets/pull.md new file mode 100644 index 000000000..2507270be --- /dev/null +++ b/snippets/pull.md @@ -0,0 +1,21 @@ +### Pull + +Use `Array.filter()` 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. +Use `Array.push()` to re-populate the original array to equal the pulled values + +```js +const pull = (arr, ...args) => { + let pulled = arr.filter((v, i) => args.indexOf(v) === -1); + 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' ] + +// let test = ['4', '1', '1', '5', '4', '2']; +// pull(test, '4', '1'); +// console.log(myArray) -> [ '5', '2' ] +```