Ya no tienes que buscar más por todo internet porque llegaste al sitio necesario, tenemos la respuesta que necesitas hallar pero sin complicaciones.
Ejemplo 1: obtener las diferencias entre dos objetos javascript lib
import transform, isEqual, isObject from'lodash';/**
* Deep diff between two object, using lodash
* @paramObjectobject Object compared
* @paramObjectbase Object to compare with
* @returnObject Return a new object who represent the diff
*/functiondifference(object, base)returntransform(object,(result, value, key)=>if(!isEqual(value, base[key]))
result[key]=isObject(value)&&isObject(base[key])?difference(value, base[key]): value;);
Ejemplo 2: 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)
Puedes ayudar nuestra tarea ejecutando un comentario y dejando una puntuación te damos la bienvenida.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)