const obj1 = {
a: "old value",
b: {
c: "old value"
},
d: 123
}
const obj2 = {
a: "old value",
b: {
c: "new value"
},
d: 321
}
const changes =
_.differenceWith(_.toPairs(obj2), _.toPairs(obj1), _.isEqual)
// Changes in array form
console.log(changes)
// Changes in object form
console.log(_.fromPairs(changes))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
Answer from Dani on Stack OverflowLodash
lodash.com › docs
Lodash Documentation
This method is like _.difference except that it accepts iteratee which is invoked for each element of array and values to generate the criterion by which they're compared. The order and references of result values are determined by the first array. The iteratee is invoked with one argument: (value).
Top answer 1 of 4
23
const obj1 = {
a: "old value",
b: {
c: "old value"
},
d: 123
}
const obj2 = {
a: "old value",
b: {
c: "new value"
},
d: 321
}
const changes =
_.differenceWith(_.toPairs(obj2), _.toPairs(obj1), _.isEqual)
// Changes in array form
console.log(changes)
// Changes in object form
console.log(_.fromPairs(changes))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>
2 of 4
0
I wanted to do the comparison between two objects but in my case, I wanted to check only some properties of the objects. So I use pick and isEqual from lodash.
const obj1 = {p1: 1, p2: 2, p3: 3};
const obj2 = {p1: 1, p2: 2, p3: 3};
const comparisonProperties = ["p1", "p2"];
console.log(_.isEqual(_.pick(obj1, comparisonProperties), _.pick(obj2, comparisonProperties))
This is how I did it.
Videos
02:43
LodashJs - Difference Method #lodash #lodashjs #javascript ...
06:09
Lodash Difference, DifferenceBy and DifferenceWith Array Methods ...
What is Lodash and How it Works (for beginners)
01:55
Understanding the Difference of Two Arrays of Objects with Lodash ...
03:06
Lodash Array Difference? - Next LVL Programming - YouTube
04:45
Lodash vs Underscore - YouTube
npm
npmjs.com › package › lodash.difference
lodash.difference - npm
August 13, 2016 - The lodash method `_.difference` exported as a module.. Latest version: 4.5.0, last published: 10 years ago. Start using lodash.difference in your project by running `npm i lodash.difference`. There are 487 other projects in the npm registry using lodash.difference.
» npm install lodash.difference
Published Aug 13, 2016
Version 4.5.0
Author John-David Dalton
Repository https://github.com/lodash/lodash
Homepage https://lodash.com/
GitHub
github.com › lodash › lodash › issues › 1867
Difference of sets (array of objects) · Issue #1867 · lodash/lodash
January 23, 2016 - I have two array of objects, like below: collection one: [{ "sku": "33220", "barcode": "", "name": "Lenovo S930 Dual SIM Mobile Phone" }, { "sku": "TN2A", "price": 73000000.0, "barcode": "8412544031648", "name": "BOTELLA TRITAN BLUE 0.75...
Author al3rez
TutorialsPoint
tutorialspoint.com › lodash › lodash_difference.htm
Lodash - difference method
Lodash - Discussion · Selected ... · _.difference(array, [values]) Creates an array of array values not included in the other given arrays using SameValueZero for equality comparisons....
GitHub
gist.github.com › Yimiprod › 7ee176597fef230d1451
Deep diff between two object, using lodash · GitHub
/** * Recursive diff between two object * @param {Object} base Object to compare with * @param {Object} object Object compared * @return {Object} Return a new object who represent the diff OR Return FALSE if there is no difference */ function differenceBetweenObjects (base,object){ let diff = false; if(typeof object == 'object'){ for(let k in object){ if(typeof object[k] != 'object'){ if(base[k] != object[k]){ if(diff == false){diff = {};} diff[k] = object[k]; } } else{ let subDiff = differenceBetweenObjects(base[k],object[k]); if(subDiff !== false){ if(diff == false){diff = {};} diff[k] = subDiff; } } } } if(typeof base == 'object'){ for(let k in base){ if(!object.hasOwnProperty(k)){ diff[k] = null; } } } return diff; }
npm
npmjs.com › package › @types › lodash.difference
@types/lodash.difference - npm
November 7, 2023 - // Generated from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/lodash/scripts/generate-modules.ts import { difference } from "lodash"; export = difference;
» npm install @types/lodash.difference
Published Nov 07, 2023
Version 4.5.9
Mastering JS
masteringjs.io › tutorials › lodash › difference
The difference() function in Lodash
April 12, 2022 - Bite-sized full stack JavaScript tutorials for pragmatic developers that get things done
DreamFactory Forum
community.dreamfactory.com › server-side scripting
Lodash. vs _. difference - Server-Side Scripting - DreamFactory Forum
March 20, 2016 - I am trimming an array this way using lodash var data_parts = lodash._.map(strData.split("|"), lodash._.trim); but I don't see the lodash. prefix in most examples that I see on websites. They almost all just use _.ma…
GitHub
github.com › lodash › lodash
lodash v4.17.23
Starred by 61.6K users
Forked by 7.1K users
Languages JavaScript 97.2% | HTML 2.3% | EJS 0.5%
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › difference
Set.prototype.difference() - JavaScript | MDN
July 10, 2025 - difference() accepts set-like objects as the other parameter. It requires this to be an actual Set instance, because it directly retrieves the underlying data stored in this without invoking any user code.
Lodash
lodash.com
Lodash
Lodash makes JavaScript easier by taking the hassle out of working with arrays, numbers, objects, strings, etc.
npm Trends
npmtrends.com › lodash.difference-vs-lodash.differencewith-vs-lodash.get-vs-object-path
lodash.difference vs lodash.differencewith vs lodash.get vs object-path | npm trends
Comparing trends for lodash.difference 4.5.0 which has 7,272,702 weekly downloads and 61,493 GitHub stars vs. lodash.differencewith 4.5.0 which has 116,646 weekly downloads and 61,493 GitHub stars vs. lodash.get 4.4.2 which has 14,135,289 weekly downloads and 61,493 GitHub stars vs. object-path 0.11.8 which has 1,848,299 weekly downloads and 1,066 GitHub stars.
Top answer 1 of 3
77
var presents = _.intersectionWith(array1, array2, _.isEqual);
var dif = _.differenceWith(array1, array2, _.isEqual);
_.differenceWith is only available since 4.0.0 lodash version
2 of 3
29
ES6 This will be enough:
array2.filter(e => !array1.includes(e));
without includes
array2.filter(e=> array1.indexOf(e) < 0);
Plunker for you