How to Compare Two Arrays Using Lodash (The Order Matters)

How to compare two arrays using lodash (the order matters)

You can do this in lodash by zipping both arrays, filtering, and than taking the last item of each pair. The comperator for intersection is that the pair is equal. The comperator for difference is that the pair are not equal.

const arr1 = [3,4,5,6,7,1,9];const arr2 = [1,3,4,6,7,5,9];
const compare = (comperator) => (arr1, arr2) => _.zip(arr1, arr2) .filter(comperator) .map(_.last);
const eq = _.spread(_.eq);
const intersection = compare(eq); const difference = compare(_.negate(eq));
console.log('intersection ', intersection(arr1, arr2));console.log('difference ', difference(arr1, arr2));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Using lodash to compare jagged arrays (items existence without order)

If you sort the outer array, you can use _.isEqual() since the inner array is already sorted.

var array1 = [['a', 'b'], ['b', 'c']];
var array2 = [['b', 'c'], ['a', 'b']];
_.isEqual(array1.sort(), array2.sort()); //true

Note that .sort() will mutate the arrays. If that's a problem for you, make a copy first using (for example) .slice() or the spread operator (...).

Or, do as Daniel Budick recommends in a comment below:

_.isEqual(_.sortBy(array1), _.sortBy(array2))

Lodash's sortBy() will not mutate the array.

How to compare objects using lodash regardless on their order

You can use the isEqual function which does a deep equal check (regardless of key order):

   _.isEqual(obj1, obj2)

See more here: https://lodash.com/docs/2.4.2#isEqual

Compare two arrays of objects excluding a property using lodash

Use the _.differenceWith() comparator function to compare the val1 property of the objects:

const arr1 = [{"id":1,"val1":{"pre":1,"foo":2}}]
const arr2 = [{"id":3,"val1":{"pre":1,"foo":2}}]

const result = _.differenceWith(arr1, arr2, (a, b) => _.isEqual(a.val1, b.val1))

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.js"></script>

Lodash / javascript : Compare two collections and return the differences

var presents = _.intersectionWith(array1, array2, _.isEqual);
var dif = _.differenceWith(array1, array2, _.isEqual);

_.differenceWith is only available since 4.0.0 lodash version

Compare two arrays containing objects in order to calculate what changed?

You could use Array.filter with Array.some, which will give you a new Array with the changed items.

Maybe something like so:

var before = [  {id: 0, name: 'Bob', age: 27},  {id: 1, name: 'Frank', age: 32},  {id: 2, name: 'Joe', age: 38}]
var after = [ {id: 0, name: 'Bobb', age: 27}, {id: 1, name: 'Frank', age: 33}, {id: 2, name: 'Joe', age: 38}]
var changed = after.filter( function( p, idx ) { return Object.keys(p).some( function( prop ) { return p[prop] !== before[idx][prop]; })})
console.log(changed)
.as-console-wrapper {  max-height: 100% !important;}

How to get changes between 2 arrays of objects? [ lodash/ JS]

You can use _.differenceWith(https://lodash.com/docs/4.17.15#differenceWith)

const first=[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 8, end: 36 }, point: 49 },
]

const second=[
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 12, end: 24 }, point: 56 },
{ id: '38sj3-23', user: { id: 21323 }, count: { start: 36, end: 97 }, point: 15 },
{ id: null, count: { start: 123, end: 2135 }, point: 323 },
]

let result=_.differenceWith(second, first, _.isEqual)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Array of object deep comparison with lodash

You can make use of differenceWith() with an isEqual() comparator, and invoke isEmpty to check if they are equal or not.

var isArrayEqual = function(x, y) {  return _(x).differenceWith(y, _.isEqual).isEmpty();};
var result1 = isArrayEqual( [{a:1, b:2}, {c:3, d:4}], [{b:2, a:1}, {d:4, c:3}]);
var result2 = isArrayEqual( [{a:1, b:2, c: 1}, {c:3, d:4}], [{b:2, a:1}, {d:4, c:3}]);
document.write([ '<div><label>result1: ', result1, '</label></div>', '<div><label>result2: ', result2, '</label></div>',].join(''));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.11.2/lodash.js"></script>


Related Topics



Leave a reply



Submit