Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

View jpsear's full-sized avatar
🚀

James Sear jpsear

🚀
View GitHub Profile
@jpsear
jpsear / functional-array-manipulation.js
Last active February 12, 2018 15:27
Functional Array Manipulation
// 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
@jpsear
jpsear / nest-flat-array.js
Last active April 13, 2019 14:28
Nest flat array that has children
const nest = (items, id = null, link = 'childId') => {
return items
.filter(item => item[link] === id)
.map(item => ({ ...item, children: nest(items, item._id) }))
}