javascript - Lodash union of arrays of objects -


i'd use _.union function create union of 2 arrays of objects. union works arrays of primitives uses === examine if 2 values equal.

i'd compare objects using key property: objects same key property regarded equal. there nice functional way achieve ideally using lodash?

a non pure lodash way using array.concat function able pretty along uniq():

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

an alternative approach use flatten() , uniq():

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

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -