Lodash union of arrays of objects

前端 未结 6 1682
臣服心动
臣服心动 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:30

    1. Compare objects using a key property:-

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

    1. Compare objects using all key property:-

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

    1. Compare objects using a string key property in case insensitive way:-

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

    propname is name of key property of your object.

提交回复
热议问题