对象数组的 Lodash 联合

Lodash union of arrays of objects

我想使用 _.union 函数创建两个对象数组的联合。 Union 仅适用于基元数组,因为它使用 === 检查两个值是否相等。

我想使用键 属性 比较对象:具有相同键 属性 的对象将被视为相等。有没有一种很好的功能性方法可以理想地使用 lodash 来实现?

一种非纯粹的 lodash 方法,但使用 array.concat 函数,您可以沿着 uniq():

非常简单地完成此操作
var objUnion = function(array1, array2, matcher) {
  var concated = array1.concat(array2)
  return _.uniq(concated, false, matcher);
}

另一种方法是使用 flatten() and uniq():

var union = _.uniq(_.flatten([array1, array2]), matcherFn);

那么 UniqBy 与之前两个数组的连接呢?

import _ from 'lodash'

const arrayUnion = (arr1, arr2, identifier) => {
  const array = [...arr1, ...arr2]

  return _.uniqBy(array, identifier)  
 }


const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }]
const array2 = [{ id: 3 }, { id: 4 }, { id: 4 }]

console.log(arrayUnion(array1, array2, 'id'))

result → [{ 'id': 1 }, { 'id': 2 }, { 'id': 3 }, { 'id': 4 }]

_.unionBy(array1, array2, matcherFn);

lodash 从数组中合并对象

const test1 = [
  { name: 'zhanghong', age: 32, money: 0, size: 12, },
  { name: 'wanghong', age: 20, size: 6 },
  { name: 'jinhong', age: 16, height: 172 },
]

const test2 = [
  { name: 'zhanghong', gender: 'male', age: 14 },
  { name: 'wanghong', gender: 'female', age: 33 },
  { name: 'lihong', gender: 'female', age: 33 },
]

const test3 = [
  { name: 'meinv' },
]

const test4 = [
  { name: 'aaa' },
]

const test5 = [
  { name: 'zhanghong', age: 'wtf' },
]

const result = mergeUnionByKey(test1, test2, test3, test4, [], test5, 'name', 'override')

function mergeUnionByKey(...args) {

  const config = _.chain(args)
    .filter(_.isString)
    .value()

  const key = _.get(config, '[0]')

  const strategy = _.get(config, '[1]') === 'override' ? _.merge : _.defaultsDeep

  if (!_.isString(key))
    throw new Error('missing key')

  const datasets = _.chain(args)
    .reject(_.isEmpty)
    .filter(_.isArray)
    .value()

  const datasetsIndex = _.mapValues(datasets, dataset => _.keyBy(dataset, key))

  const uniqKeys = _.chain(datasets)
    .flatten()
    .map(key)
    .uniq()
    .value()

  return _.chain(uniqKeys)
    .map(val => {
      const data = {}
      _.each(datasetsIndex, dataset => strategy(data, dataset[val]))
      return data
    })
    .filter(key)
    .value()

}

console.log(JSON.stringify(result, null, 4))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

晚会迟到了,但 _.unionWith 做自己想做的事情要好得多。

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }];
var others = [{ 'x': 1, 'y': 1 }, { 'x': 1, 'y': 2 }];

_.unionWith(objects, others, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 1 }]
  1. 使用键比较对象属性:-

_.unionBy(array1,array2,'propname');

  1. 使用所有键比较对象属性:-

_.unionWith(array1,array2, _.isEqual);

  1. 使用不区分大小写的字符串键 属性 比较对象:-

_.unionWith(array1,array2, function(obj,other){ return obj.propname.toLowercase() === other.propname.toLowercase(); });

propname 是对象的键名 属性。

_.spread(_.merge) 就可以了。

_.spread(_.merge)([{ a: 1 }, { b: 2 }, { c: 3 }])

会 return { a: 1, b: 2, c: 3 }