Lodash union of arrays of objects

前端 未结 6 1683
臣服心动
臣服心动 2020-12-10 02:57

I\'d like to use the _.union function to create a union of two arrays of objects. Union works with arrays of primitives only as it uses === to examine if two va

6条回答
  •  一整个雨季
    2020-12-10 03:52

    A non pure lodash way to do this but using the array.concat function you are able to do this pretty simply along uniq():

    var objUnion = function(array1, array2, matcher) {
      var concated = array1.concat(array2)
      return _.uniq(concated, false, matcher);
    }
    

    An alternative approach would be to use flatten() and uniq():

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

提交回复
热议问题