From 957f4789820152d2bfd10a1451aa4402664a5629 Mon Sep 17 00:00:00 2001 From: King Date: Mon, 18 Dec 2017 17:42:47 -0500 Subject: [PATCH 1/3] add pullAtIndex.md --- snippets/pullAtIndex.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 snippets/pullAtIndex.md diff --git a/snippets/pullAtIndex.md b/snippets/pullAtIndex.md new file mode 100644 index 000000000..e9efc172d --- /dev/null +++ b/snippets/pullAtIndex.md @@ -0,0 +1,24 @@ +### pullAtIndex + +This method is like pull except that it accepts an array of indexes to filter out before Mutating and pulling all the values from the original array. Which then returns an array of removed elements. + +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. +Use `Array.push()` to keep track of pulled values + +```js +const pullAtIndex = (arr, pullArr) => { + let removed = []; + let pulled = arr.map((v, i) => pullArr.includes(i) ? removed.push(v) : v) + .filter((v, i) => !pullArr.includes(i)) + arr.length = 0; + pulled.forEach(v => arr.push(v)); + return removed; +} + +// let myArray = ['a', 'b', 'c', 'd']; +// let pulled = pullAtIndex(myArray, [1, 3]); + +// console.log(myArray); -> [ 'a', 'c' ] +// console.log(pulled); -> [ 'b', 'd' ] +``` \ No newline at end of file From 06e61e311607dcf5dd33b43325df4968e53d8187 Mon Sep 17 00:00:00 2001 From: King Date: Mon, 18 Dec 2017 17:44:18 -0500 Subject: [PATCH 2/3] update tag DB with pullAtIndex:array & ran npm run tagger --- tag_database | 1 + 1 file changed, 1 insertion(+) diff --git a/tag_database b/tag_database index f48f069b3..c1a503097 100644 --- a/tag_database +++ b/tag_database @@ -71,6 +71,7 @@ pipe:function powerset:math promisify:function pull:array +pullAtIndex:array randomIntegerInRange:math randomNumberInRange:math readFileLines:node From 4110110a1b7d068b186a4998dd95547cf1fc592b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Tue, 19 Dec 2017 13:24:57 +0200 Subject: [PATCH 3/3] Update pullAtIndex.md --- snippets/pullAtIndex.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/snippets/pullAtIndex.md b/snippets/pullAtIndex.md index e9efc172d..0e0d8cf7f 100644 --- a/snippets/pullAtIndex.md +++ b/snippets/pullAtIndex.md @@ -1,6 +1,6 @@ ### pullAtIndex -This method is like pull except that it accepts an array of indexes to filter out before Mutating and pulling all the values from the original array. Which then returns an array of removed elements. +Mutates the original array to filter out the values at the specified indexes. 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. @@ -11,7 +11,7 @@ const pullAtIndex = (arr, pullArr) => { let removed = []; let pulled = arr.map((v, i) => pullArr.includes(i) ? removed.push(v) : v) .filter((v, i) => !pullArr.includes(i)) - arr.length = 0; + arr.length = 0; pulled.forEach(v => arr.push(v)); return removed; } @@ -21,4 +21,4 @@ const pullAtIndex = (arr, pullArr) => { // console.log(myArray); -> [ 'a', 'c' ] // console.log(pulled); -> [ 'b', 'd' ] -``` \ No newline at end of file +```