Skip to content

Instantly share code, notes, and snippets.

@CharlieYe0205
Last active August 21, 2019 07:18
Show Gist options
  • Save CharlieYe0205/4ec252d670e30c933f2fe873261d0b30 to your computer and use it in GitHub Desktop.
Save CharlieYe0205/4ec252d670e30c933f2fe873261d0b30 to your computer and use it in GitHub Desktop.
Lodash Fun

map

_.map(users, 'name')

filter

_.filter(users, {name: 'John'}) # get all users whose name is John, return array

find

_.find(users, {name: 'John'}) # get first user whose name is John, return element

remove

this will mutate the origin data

reject

_.reject(users, {id:2})

every and some

_.every(users, {active: true})
_.some(users, {active: true})

sort

_.orderBy(initUsers, ['likes', 'name'], ['desc', 'asc'])

chain

_.chain(users)
	.filter('active')
    .orderBy(['likes'])
    .map(function(user){
    	return user.name + ' has ' + user.likes + ' likes.'
    })
    .head()
    .value()

head <-> last

initial <-> tail

Happy Coding ^_^
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment