This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| flattenDeep([]) // => [] | |
| flattenDeep([[1]]) // => [1] | |
| flattenDeep([1,[[[2],22,[222]]],3, () => {}, { a: 4 }, [undefined, [null]], null, 'hi']) | |
| // => [1, 2, 22, 222, 3, () => (), {a: 4}, undefined, null, null, 'hi'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| range(0, 20, 5) // => [0, 5, 10, 15] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function range(start, end, step) { | |
| const results = []; | |
| if (end === undefined) { | |
| end = start; | |
| start = 0; | |
| } | |
| if (step === undefined) { | |
| step = (start < end) ? 1 : -1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| range(4) // => [0, 1, 2, 3] | |
| range(-4) // => [0, -1, -2, -3] | |
| range(1, 5) // => [1, 2, 3, 4] | |
| range(0, 20, 5) // => [0, 5, 10, 15] | |
| range(0, -4, -1) // => [0, -1, -2, -3] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const pizzaSlices = ['pepperoni', 'pepperoni', 'olives', 'olives', 'plain', 'plain', 'bacon', 'bacon']; | |
| chunk(pizzaSlices, 2); // => [['pepperoni', 'pepperoni'], ['olives', 'olives'], ['plain', 'plain'], ['bacon', 'bacon']]; | |
| chunk(pizzaSlices, 3); // => [['pepperoni', 'pepperoni', 'olives'], ['olives', 'plain', 'plain'], ['bacon', 'bacon']]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sumBy(items, iteratee) { | |
| const sumByFunc = getSumByFunc(iteratee); | |
| let sum = 0; | |
| items.forEach(item => { | |
| sum += sumByFunc(item); | |
| }); | |
| return sum; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sumBy(items, iteratee) { | |
| const sumByFunc = getSumByFunc(iteratee); | |
| return items.reduce((sum, item) => { | |
| return sum + sumByFunc(item); | |
| }, 0); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sumBy(items, iteratee) { | |
| const sumByFunc = getSumByFunc(iteratee); | |
| // sum loop... | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function sumBy(items, iteratee) { | |
| const sumByFunc = getSumByFunc(iteratee); | |
| let sum = 0; | |
| for(let i = 0; i < items.length; i++) { | |
| sum += sumByFunc(items[i]); | |
| } | |
| return sum; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function getSumByFunc(iteratee) { | |
| if (iteratee === undefined || iteratee === null) return item => item; | |
| if (typeof iteratee === 'string') return item => item[iteratee]; | |
| return iteratee; | |
| } |
NewerOlder