Native usage
APIs for direct use
_.reject
Removes an element based on the condition.
var foo = [ {id: 0, name: "aaa", age: 33}, {id: 1, name: "bbb", age: 25}]var bar = _.reject(foo, [‘id‘, 0])//bar = [{id: 1, name: "bbb", age: 25}]
_.pick
Filters the value in the first parameter and returns the array based on the key of the second parameter
var foo = {id: 0, name: "aaa", age: 33}var bar = _.pick(foo, [‘name‘, ‘age‘])//bar = {name: "aaa", age: 33}
_.keys
Returns all keys in Object
var foo = {id: 0, name: "aaa", age: 33}var bar = _.keys(foo)//bar = [‘id‘, ‘name‘, ‘age‘]
_.clonedeep
Deep copy, this needless to say, JS in the type other than the underlying type, will default copy backup
var bar = _.cloneDeep(foo)
_.find
Finding arrays
var foo = [ {id: 0, name: "aaa", age: 33}, {id: 1, name: "bbb", age: 25}]var bar = _.find(foo, [‘id‘, 0])//bar = {id: 0, name: "aaa", age: 33}
Note that if not found, will return to undefined, to deal with
_.keyby
To convert an array to an object with a property as the key
var foo = var foo = [ {id: 0, name: "aaa", age: 33}, {id: 1, name: "bbb", age: 25}]var bar = _.keyBy(foo, ‘name‘)//bar = {// aaa: {id: 0, name: "aaa", age: 33},// bbb: {id: 1, name: "bbb", age: 25}//}
_.filter
Filters out eligible elements based on conditions, returns a new array
var foo = var foo = [ {id: 0, name: "aaa", age: 33}, {id: 1, name: "bbb", age: 25}]var bar = _.filter(foo, [‘name‘, "aaa"])//bar = {// aaa: {id: 0, name: "aaa", age: 33}//}
_.map
Pick a key from the collection and return its value as an array
var foo = var foo = [ {id: 0, name: "aaa", age: 33}, {id: 1, name: "bbb", age: 25}]var bar = _.map(foo, ‘name‘)//bar = ["aaa", "bbb"]
_.max/_.min/_.sum
Maximum, minimum, array sum in array
var foo = [1, 2, 3, 4]var bar = _.max(foo)//bar = 4bar = _.min(foo)//bar = 1bar = _.sum(foo)//bar = 10
_.pad/_.padstart/_.padend
Padded characters at both ends, at the beginning, and at the end
var foo = "helloworld"var bar = _.pad(foo, 14, ‘-‘)//bar = --helloworld--bar = _.padStart(foo, 14, ‘-‘)//bar = ----helloworldbar = _.padEnd(foo, 14, ‘-‘)//bar = helloworld----
Combination usage
If that's the basic skill, here are a few cool combos:
Select one of the largest IDs in the JSON array
var foo = [ {id: 0, name: "aaa", age: 33}, {id: 1, name: "bbb", age: 25}]var bar = _.find(foo, [‘id‘, _.max(_.map(foo, ‘id‘))])// bar = {id: 1, name: "bbb", age: 25}
PS: can also use Maxby a key to replace
Update the value of an item in a JSON array
var foo = [{id: 0, name: " AAA ", age: 33}, {id: 1, name: Span class= "hljs-string" > "BBB", age: 25}] let list = _.keyby (foo, ' id ') list[0 ].name = "CCC" var bar = _.map (list) bar = [//{id:0, Name: "CCC", Age:33},//{id:1, Name: "BBB", a Ge:25}//]
[go] lodash Common API notes