Lodash方法测试

版权声明:本文为个人知识整理,欢迎指正、交流、转载。 https://blog.csdn.net/u014711094/article/details/82011701

1 Array

difference intersection union xor

_.difference([0, 1, 2, 3, 1, 2], [-0, 2])	// [1, 3, 1]

_.differenceBy([1.2, 2.2], [0.1, 1.1], Math.floor)	// [2.2]
_.differenceBy([{x: 1, y: 1}, {x: 2, y: 2}], [{x: 1}], 'x')		
// [{x: 2, y: 2}]

_.differenceWith([{x: 1}, {x: 2}], [{x: 1}], _.isEqual)
// [{x: 2}]
_.differenceWith([{x: 1}, {x: 2}], [{y: 1}], (v1, v2) => v1.x===v2.y)
// [{x: 2}]

// 等价
_.differenceBy(arr, vals, fn)
_.differenceWith(arr, vals, (v1, v2) => fn(v1) === fn(v2))

_.differenceBy(arr, vals, 'x')
_.differenceWith(arr, vals, (v1, v2) => v1.x === v2.x)

// intersection union xor会去重
_.intersection([1, 1, 2, 3], [1, 1, 4])		// [1]
_.union([1, 1, 2, 3], [1, 1, 4])			// [1, 2, 3, 4]
_.xor([1, 1, 2, 2, 3], [1, 1, 4])			// [2, 3, 4]

drop take

// drop dropWhile dropRight dropWhileRight
Array.prototype.splice

// take takeWhile takeRight takeWhileRight
Array.prototype.slice

flatten flattenDeep flattenDepth

let arr = [1, [2, [3, [4, 5]]]]

_.flatten(arr)			// [1, 2, [3, [4, 5]]]
_.flattenDeep(arr)		// [1, 2, 3, 4, 5]
_flattenDepth(arr, 2)	// [1, 2, 3, [4, 5]]

zip zipWith zipObject zipObjectDeep

let arr = [['a', 'b'], [1, 2], [true, false]]

_.zip(...arr)
// [['a', 1, true], ['b', 2, false]]

_.zipWith(...arr, (x, y, z) => x + y + z)
// ['a1true', 'b2false']

_.zipObject(...arr)
// {a: 1, b: 2}

_.zipObjectDeep(['a.b[0].c', 'a.b[1].c'], [1, 2])
// {a: {b: [{c: 1}, {c: 2}]}}

2 Object

function Foo() {
  this.a = 1
  this.b = 2
}
Foo.prototype.c = 3

let foo = new Foo()

pick pickby omit omitby

let ks = ['a', 'c', 'd']

_.pick(foo, ks)		// {a: 1, c: 3}
_.omit(foo, ks)		// {b: 2}

let predicate = (v, k) => v > 1 && k < 'c'
_.pickBy(foo, predicate)	// {b: 2}
_.omitBy(foo, predicate)	// {a: 1, c: 3}

findKey findLastKey

_.findKey(foo, (v, k, o) => v > 1)		// 'b'

// 通过Object.entries()实现
let entries = Object.entries(foo)
let k

for(let i = 0; i < entries.length; i++) {
  if (entries[i][1] > 1) {
    k = entries[i][0]
    break
  }
}

let users = {
  'barney':  { 'age': 36, 'active': true },
  'fred':    { 'age': 40, 'active': false },
  'pebbles': { 'age': 1,  'active': true }
}

// 上面是下面的简写
_.findKey(user, { 'age': 40, 'active': false })
_.findKey(user, _.matches({'age': 40, 'active': false}))

_.findKey(user, ['age', 40])
_.findKey(user, _.matchesProperty(['age', 40]))

_.findKey(user, 'age')
_.findKey(user, _.property('age'))

mapKeys mapValues()

let o = {a: 1, b: 2}

_.mapKeys(o, (v, k, o) => k.toUpperCase())
// {A:1, B: 2}

_mapValues(o, (v, k, o) => v + 1)
// {a: 2, b: 3}

3 Lang

clone cloneDeep

let arr = [{a: 1}, {a: 2}]

_.clone(arr)[0] === arr[0]			// true
_.cloneDeep(arr)[0] === arr[0]		// false

isObject isObjectLike isPlainObject

let fn = () => 1
let s = new String('a')

_.isObject(null)				// false
_.isObject(fn)					// true
_.isObject(s)					// true

-- 要求typeof的结果是'object'
_.isObjectLike(null)		// false
_.isObjectLike(fn)			// false
_.isObjectLike(s)			// true

_.isPlainObject(null)		// false
_.isPlainObject(fn)			// false
_.isPlainObject(s)			// false

_.isPlainObject({a: [1, 2]})					// true
_.isPlainObject(Object.create(null))			// true

4 Misc

// data for test
let o = {a: 1, b: 2, c: 3}

find findIndex findKey

_.find(o, v => v > 1)		// 2
_.findKey(o, v => v> 1)		// c

map

_.map(o, (v, k, o) => k + v)
// ['a1', 'b2', 'c3']

猜你喜欢

转载自blog.csdn.net/u014711094/article/details/82011701