showing results for - "object difference javascript"
Chayma
03 Aug 2020
1import { transform, isEqual, isObject } from 'lodash';
2
3/**
4 * Deep diff between two object, using lodash
5 * @param  {Object} object Object compared
6 * @param  {Object} base   Object to compare with
7 * @return {Object}        Return a new object who represent the diff
8 */
9function difference(object, base) {
10	return transform(object, (result, value, key) => {
11		if (!isEqual(value, base[key])) {
12			result[key] = isObject(value) && isObject(base[key]) ? difference(value, base[key]) : value;
13		}
14	});
15}
Charlotte
08 May 2016
1const { inspect } = require('util')
2const { transform, isEqual, isArray, isObject } = require('lodash')
3
4/**
5 * Find difference between two objects
6 * https://davidwells.io/snippets/get-difference-between-two-objects-javascript
7 *
8 * @param  {object} origObj - Source object to compare newObj against
9 * @param  {object} newObj  - New object with potential changes
10 * @return {object} differences
11 */
12function difference(origObj, newObj) {
13  function changes(newObj, origObj) {
14    let arrayIndexCounter = 0
15    return transform(newObj, function (result, value, key) {
16      if (!isEqual(value, origObj[key])) {
17        let resultKey = isArray(origObj) ? arrayIndexCounter++ : key
18        result[resultKey] =
19          isObject(value) && isObject(origObj[key])
20            ? changes(value, origObj[key])
21            : value
22      }
23    })
24  }
25  return changes(newObj, origObj)
26}
27
28/* Usage */
29
30const originalObject = {
31  foo: 'bar',
32  baz: 'fizz',
33  cool: true,
34  what: {
35    one: 'one',
36    two: 'two'
37  },
38  wow: {
39    deep: {
40      key: ['a', 'b', 'c'],
41      values: '123'
42    }
43  },
44  array: ['lol', 'hi', 'there']
45}
46
47const newObject = {
48  foo: 'bar',
49  baz: 'fizz',
50  cool: false, // <-- diff
51  what: {
52    one: 'one',
53    two: 'twox' // <-- diff
54  },
55  wow: {
56    deep: {
57      key: ['x', 'y', 'c'], // <-- diff
58      values: '098' // <-- diff
59    }
60  },
61  array: ['lol', 'hi', 'difference'] // <-- diff
62}
63
64// Get the Diff!
65const diff = difference(originalObject, newObject)
66
67console.log(inspect(diff, {showHidden: false, depth: null, colors: true}))
68
69// result:
70// {
71//   cool: false,
72//   what: { two: 'twox' },
73//   wow: { deep: { key: [ 'x', 'y' ], values: '098' } },
74//   array: [ 'difference' ]
75// }
76
77
78if (diff.cool) {
79  console.log('Coolness changed to', diff.cool)
80}
81
queries leading to this page
js get diff of nested objectsdifference between objects and hash taleshow differences between two js objectysjs difference between two objectsdiff objects javascriptjavascript object differencecompare two objects and find the differences javascriptcompare differences between two objects jstwo object difference jsjavascript compare object differencediff objects in javascriptcheck difference between two nested objects jsjs diff objectsfind difference of two object in jsget diff of objects javascriptget difference between two objects javascriptcheck for changes in two objects javascript es6javascript deep compare two objectnode show difference of 2 objectsjavascript check difference between two objectsjs get difference between two objects deepreturn difference between objects javascriptdifference between object this and thisjavascript check if object is different and get differencejavascript difference between two objectsfind objects differencestypescript diff objectsjavascript find difference between two objectssee changes in two objects jsjavascript find differences between two objectsnode js javascript object differencecheck two object differences tsget diff between objects jssee difference between two objects jsget object which is the difference of two objects jsjavascript display differnces between two objectsjavascript object differencejs diff objectdifference between object and function in javascriptdiff between two objectsjs difference between item objecthow to obtain the difference between two objects in jsdiff value tow object jsjavascript difference between objectsjs find difference properties valuesjs difference between objectsdifference between two objectscompare two objects and get the difference in two objects using jscheck two object differences jsjavascript compare complex objects and get differencesdiffrence of two objects javascriptdraw line between two objects and track distance between themhow to output the difference in two js objects 22typescript 22 object diffobject check for differencescompare two objects and find differences javascriptget difference between two objects as object javascript javascript get data difference between two objectsget two object difference jsjavascript compare objects and return differencediff between 2 js data js check object difference onlinecompare between two object javascriptjavascript difference between object and varjs find objects differencevariable and object difference in jsjs find differences between two objects valuesjs check object differencegetting the differences between two objects javascript libcompare all values between two objects javascriptget the delta between two objects jsdifference between 2 objects javascriptjavascript difference between objectjavascript find object differencesdiff between two objects 2bbenchmarkget object difference javascriptjavascript difference of two objectsvar and object difference fidn diff of 2 object and update the new onehow i compare between two objects in javascriptdiff javascript object get updateddiff javascript object get updateget values which are different between two objectdeep diff array orderdifference of object in java and javascriptjs get difference between two objectsdiffbetween 2 js bojectsjavascript new object includes changes and between two objectsfunction to find difference in objects javascriptjavascript compare objects differencetwo objects log the differencewhat is difference between java object and javascript objectjavascript compare 2 objects and get differencesjs get diff of two objectsvariable and object differencediff 2 objects javascriptget the difference between to objects jsjavascript differences between two objectsfind difference between two object jsjavascript compare two objects and get differencesjavascript getting the different fields between two objectsjavascript compare two objects for changesdiffing two objects jscompare two object values get the difference javascripttwo objects log the difference typescriptjavascript difference between object and variablejavascript how to compare object differencejs difference between object and objectget differences between two objects jshow to compare two objects in javascript and get differencesangular compare objects diffjavascript get differences between two objectsnode js many objects vs js objects diffget value changes two objectsuse two objects in find one functionjs diff between objectsnode diff two objectsnode js check diff between objectshow to find object difflook differences between 2 objects javascriptjs find what is difference between two objectsdiffing two objectjavascript how to find difference in objectsdiff of object javascriptdifference between creating object in javascriptobject difference javascriptdifference between and 5b 5d in objects jsjavascript difference between two objects keysdiff between two object in jscompare js objects differencesget the difference between two objects jsjs find difference properties between objectsobject vs object javascriptjavascript difference of objectsdeep diif same arraydifferences between to objectget value changes two objects jsfind difference between two objects javascriptjavascript show difference between objectsfind all differences of 2 objects in javascriptjs differences between two objectscompare two javascript objects for changessee object differencehow to find the difference between two objects in javascriptdiff two objects javascriptnew object includes changes and between two objects key into object javascript differenceget difference between objects javascriptdiff between two objects javascriptobject see differences between twojs find differences between two objectsdeep object difference jscheck the difference between two objects in javascriptsee diffrence in 2 objects nodefind objects differences javascriptjs get the diff between 2 objects deepobject differencejavscript difference between two objectsget diff of object jsjs what is the difference between object and new objectfind difference between two objects jswhat are the advantages and disadvantages of using views in sqljs object differencehow to find difference between two objects in javascriptreturn difference between two objects javascriptdifference between object and variable in javascriptjs find not deep difference between two objectschecking for diff in 2 objects using nodejses6 difference between two objectsfind difference between two objectsjs show difference between objectsdiff javascript objectsjavascript find object differences valuescompare 2 object and get diffrence javascript compare between function of objectbest way to check difference two objects javascriptcompare two objects javascript show diffwhat the difference between and 5b 5d in object in jsjavascript object diffget object difference jshow to compare objects and return the difference javascripttypescript get difference between two objectscompare two objects and return the difference javascriptjs diff on two js objectsjavascript object difference 22 5b 5d 22 22 22difference of object into new obj jsdiff between two objects benchmarkshow change between two objectsdifference bet class v c3 a0 objectget difference between objects jsdifference of object create with 7b 7d in jsangular difference between objectsjs find differences between objectsjavascript objects differencejs diff 2 objectsjs find difference between two objectsjs get diff between two objects 3d 3d 3d between two objects value or referencejavascript find differences in two objectsjavascript compare two objects return differencefind differences between two objects javascriptfind difference between objects javascriptdiff between objectsjs object differencesjs object diffjs compare to objects and return the differencevar and object difference in javascript with examplegenerate difference object in javascriptcompare two objects length and return the difference javascripthow to find the difference between two objects in vuejsjavascript compare two objects and get differences javascriptjavascript get difference between two objectsjs difference between objectget the difference between two objects in javascriptjs see diff between objectjavascript diff two objectsrecat get difernece betwweb objectobject difference javascript