Hola, tenemos la solución a lo que buscabas, deslízate y la encontrarás a continuación.
Ejemplo 1: diferencia de dos objetos javascript
const inspect =require('util')const transform =require('lodash.transform')const isEqual =require('lodash.isequal')const isArray =require('lodash.isarray')const isObject =require('lodash.isobject')/**
* Find difference between two objects
* @paramobjectorigObj - Source object to compare newObj against
* @paramobjectnewObj - New object with potential changes
* @returnobject differences
*/functiondifference(origObj, newObj)functionchanges(newObj, origObj)let arrayIndexCounter =0returntransform(newObj,function(result, value, key)if(!isEqual(value, origObj[key]))let resultKey =isArray(origObj)? arrayIndexCounter++: key
result[resultKey]=(isObject(value)&&isObject(origObj[key]))?changes(value, origObj[key]): value
)returnchanges(newObj, origObj)/* Usage */const originalObject =
foo:'bar',
baz:'fizz',
cool:true,
what:
one:'one',
two:'two',
wow:
deep:
key:['a','b','c'],
values:'123',
array:['lol','hi','there']const newObject =
foo:'bar',
baz:'fizz',
cool:false,// <-- diff
what:
one:'one',
two:'twox'// <-- diff,
wow:
deep:
key:['x','y','c'],// <-- diff
values:'098'// <-- diff,
array:['lol','hi','difference']// <-- diff// Get the Diff!const diff =difference(originalObject, newObject)console.log(inspect(diff,showHidden:false, depth:null, colors:true))/* result:
cool: false,
what: two: 'twox' ,
wow: deep: key: [ 'x', 'y' ], values: '098' ,
array: [ 'difference' ]
*/if(diff.cool)console.log('Coolness changed to', diff.cool)
Ejemplo 2: lodash deep compare dos objetos
var bob ="sex":"male","age":21;var bill ="sex":"male","age":21;if(_.isEqual(bob, bill))//we are equalelse//we are not equal
Tienes la opción de añadir valor a nuestro contenido cooperando tu veteranía en las notas.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)