javascript - distinct objects between two arrays based on a property - lodash -


is there direct method distinct objects into new array based on object's property 2 different arrays ?

currently looping through first array , comparing each object in second array maintain counter , push new array, when distinct.

   var newarray = [];    _.each(firstarray, function(item) {               //loop items in 1st []       var count = 0;              _.each(secondarray, function(seconditem) {        if(seconditem.property = item.property) {               count++        }      }     if(count == 0)   newarray.push(item); //push distinct item new []   } 

example data :

var array1 = [{id:1, busnum: "1234"}, {id:2, busnum:"4567"}] var array2 = [{id:1, busnum: "2344"}, {id:2, busnum:"1234"}] 

want eliminate common buses (ones same bus number)

  var newarray = [{id:2, bus:"4567"}, {id:1, bus:"2344"] 

you use _.xorby symmetric difference.

var array1 = [{id:1, busnum: "1234"}, {id:2, busnum:"4567"}],      array2 = [{id:1, busnum: "2344"}, {id:2, busnum:"1234"}],      result = _.xorby(array1, array2, 'busnum');    console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>


Comments

Popular posts from this blog

c# - Update a combobox from a presenter (MVP) -

How to understand 2 main() functions after using uftrace to profile the C++ program? -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -