Last active
February 12, 2018 15:27
-
-
Save jpsear/212d2b3b8bdb947268f1aad19f230617 to your computer and use it in GitHub Desktop.
Functional Array Manipulation
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
// Return a new array minus a given item | |
var a = [1, 2, 3, 4, 5] | |
var b = a.filter(item => item !== 2) // [1, 3, 4, 5] | |
// Return true if a value is in an array | |
var a = [1, 2, 3, 4, 5] | |
var b = a.some(item => item === 1) // true | |
var b = a.some(item => item === 6) // false | |
// Return the full item if a value is in an array | |
var a = [1, 2, 3, 4, 5] | |
var b = a.find(item => item === 1) // 1 | |
var b = a.find(item => item === 6) // undefined | |
// Transform array of objects into a new array of new values | |
var a = [{ id: '1'}, { id: '2'}, { id: '3'}] | |
var b = a.reduce((arr, item) => arr.concat(item.id), []) // ['1', '2', '3'] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment