Exercises: https://karisantos.github.io/book/learning/week3/lodash-drills.html
— by Abhisek Pattnaik <[email protected]>
Last active
October 19, 2017 06:03
-
-
Save abhisekp/d6914d5c8e946772e2032646ee7261b3 to your computer and use it in GitHub Desktop.
Lodash Methods
isNumber
- check fornumber
orNaN
valueisMap
- check forMap
typeisFunction
- check forfunction
valueisNull
- check fornull
valueisNil
- check fornull
orundefined
valueisNaN
- check forNaN
valueisError
- check forError
typeisSet
- check forSet
typeisDate
- check forDate
type
take
- takes then
number of elements from lefttakeRight
- takes then
number of elements from righttakeWhile
- starts takes elements from left till predicate function is truthytakeRightWhile
- starts takes elements from right till predicate function is truthyslice
first
orhead
- gets the first elementlast
- gets the last elementinitial
- gets all but last elementtail
- gets all but first element
const obj = {
id: '123',
from: {
user: 'abhisekp'
},
'from.user': 'Tommy'
}
console.log(
pickDeepAll(['id', 'from.user', {
from_user: 'from.user'
}], obj)
);
/*
{
from.user: "Tommy",
from_user: "Tommy",
id: "123"
}
*/
console.log(
pickDeepAll('id', obj)
);
/*
{
id: "123"
}
*/
const pickDeepAll = _.curry((props, obj) => {
const propList = _.concat([], props);
const [propStrings, propObjs] = _.partition(_.isString, propList);
const picked1 = _.pickAll(propStrings, obj);
const picked2 = _.reduce((pickedObj, propMap) => {
const mappedObj = _.mapValues(_.get(_, obj), propMap);
return Object.assign(pickedObj, mappedObj);
}, {}, propObjs);
return Object.assign(picked1, picked2);
});
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 arr = [ | |
"0002-0002-0003-0004", | |
"5874-1734-1294-0004", | |
"4234-9999-1234-1234", | |
"4346-1234-1004-5884", | |
"0002-0002-0004-0003" | |
] | |
/* | |
1. split the array elements at `-` to 2D array | |
[["0002","0002","0003","0004", ..., ...] | |
2. Parse each inner array elements to numbers | |
[[2, 2, 3, 4], ..., ...] | |
3. Sum each array | |
[11, 8906, 16701, 12468, 11] | |
*/ | |
const _ = require('lodash/fp'); | |
const sums = _.map(_.flow( | |
_.split('-'), | |
_.map(_.toNumber), | |
_.sum | |
)); | |
console.log(sums(arr)); | |
//=> [ 11, 8906, 16701, 12468, 11 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment