Get
Import {get} from 'lodash' import {get} from 'lodash' import {get} from 'lodash' Const a1 = get(nextProps, 'treeVal', undefined) const a1 = get(nextProps, 'treeVal', undefined)Copy the code
1. Get the last element in the array
let arr = [1.2.3.4.5]
let lastElement = _.last(arr)
console.log(lastElement) / / 5
Copy the code
2. Get the penultimate element of the array
let arr = [1.2.3.4.5]
let lastSecondElement = _.nth(-2)
console.log(lastSecondElement) / / 4
Copy the code
3. To heavy
let arr = [2.1.2.'2'.true]
let uniqArr = _.uniq(arr)
console.log(uniqArr) // [2, 1, '2', true]
Copy the code
4. Sort (array of objects, sorted in ascending or descending order by the value of an attribute in the object)
let users = [
{user: 'Tom'.age: 25},
{user: 'Amy'.age: 23},
{user: 'Perter'.age: 22},
{user: 'Ben'.age: 29}]let sortedUsers = _.orderBy(users, 'age'.'desc')
console.log(sortedUsers)
// [{user: "Ben", age: 29}, {user: "Tom", age: 25}, {user: "Amy", age: 23}, {user: "Perter", age: 22}]
Copy the code
5. Remove a layer
let address = {
Jiangsu Province: [Nanjing City.'Suzhou'].'Zhejiang': ['Hangzhou'.'Shaoxing']}let cities = _.flatten(_.values(address))
console.log(cities) // [" Nanjing ", "Suzhou "," Hangzhou ", "Shaoxing "]
Copy the code
Create object {x: 10, y: 10} from array [” x “, “y”] and array [10, 10]
let keys = ["x"."y"]
let values = [10.10]
let obj = _.zipObject(keys, values)
console.log(obj) // {x: 10, y: 10}
Copy the code
7. Merge key-value pairs of multiple objects with the same key value
let obj1 = {'9': {name: 'Tesco supermarket'}}
let obj2 = {'9': {storeToken: 'xxx'}}
let obj3 = {'9': {storePosition: 'Hangzhou'}}
let mergedObj = _.merge(obj1, obj2, obj3)
console.log(mergedObj)
// 9: {name: "Tesco supermarket ", storeToken:" XXX ", storePosition: "Hangzhou"}
Copy the code
8. Merge objects with the same value of a key
.groupBy(collection, [iteratee=.identity])
_.groupBy(array, 'dc') // Merge items of the same DC together
Copy the code
9. Delete a key
_.omit(object, [props])
var object = { 'a': 1.'b': '2'.'c': 3 };
_.omit(object, ['a'.'c']);
// => { 'b': '2' }
Copy the code
10.intersection
// Take the intersection of two arrays. The return value of this method is still an array, or an empty array if there is no intersection
const arr = intersection(nowCodes, pCodeList)
Copy the code
11.intersectionBy
// In the case of nested JSON data, fetch the intersection of two arrays according to the specified attribute ID (which can be arbitrarily specified according to actual business needs). The new array is the intersection of two arrays
const arr = intersectionBy(delArr1, delArr2, 'id')
Copy the code
12.cloneDeep
// This method creates a new space in memory for the new variable. This method is essentially different from lodash's clone method, destruct assignment, object.assign, etc. These methods simply add a reference to the source data. It does not create a new space in memory. If only a shallow copy of a reference is made, then changes to the newly copied data will cause changes to the source data
const tree = cloneDeep(treeData)
Copy the code
13.throttle
// throttling function
throttle(function () {
// The callback function})},1000, { leading: true.trailing: false })
// This function takes three arguments: the first is the callback to execute, the second is the wait time, and the third is Object
//{leading: true, trailing: false}, execute once before throttling starts, and no more after the throttling time reaches
//{leading: false, trailing: true}, not executed before the throttling starts, but again after the throttling time reaches
//{leading: true, trailing: true}, which specifies that the call is executed before the throttling starts and again after the throttling time
// The throttling function is used in many business scenarios to prevent the user from clicking too often. The function can be triggered too frequently.
Copy the code
14.join
_.join(['a'.'b'.'c'].'~');
// => 'a~b~c'
Copy the code