J'ai un tableau d'objet qui ressemble à, maintenant, j'ai un MAINTENANT, CLIQUEZ Cliquez sur une fonction TRGGERD qui assignera ce tableau d'objet à l'observable. P> observable code> qui est p>
setAction(givenobject) {
//Here I am trying to check wheather the coming object is same as that of previous(which is the observable) if both are same then do not update or else update.
if(givenobject !== values)
this. values = givenobject
}
3 Réponses :
Voici mon objet comparer la fonction
p>
const compare = (obj1, obj2) => Array.isArray(obj1) ? Array.isArray(obj2) && obj1.length === obj2.length && obj1.every((item, index) => compare(item, obj2[index])) : obj1 instanceof Date ? obj2 instanceof Date && obj1.getDate() === obj2.getDate() : obj1 && typeof obj1 === 'object' ? obj2 && typeof obj2 === 'object' && Object.getOwnPropertyNames(obj1).length === Object.getOwnPropertyNames(obj2).length && Object.getOwnPropertyNames(obj1).every(prop => compare(obj1[prop], obj2[prop])) : obj1 === obj2; const obj1 = [{ a: "10", b: "20" }, {a: "30", b: "40"}, {a: "50", b: "60"}]; const obj2 = [{ a: "10", b: "20" }, {a: "30", b: "40"}, {a: "50", b: "60"}] const obj3 = [{ a: "10" }, {a: "30", b: "40"}]; console.log('obj1 equals obj2 is', compare(obj1, obj2)); console.log('obj1 equals obj3 is', compare(obj1, obj3));
Vous pouvez essayer ce code, cela fonctionnera également avec des objets profonds.
const compareArrObjects = _.isEmpty(_.differenceWith(givenobject , values , _.isEqual)) setAction(givenobject) { //Here I am trying to check wheather the coming object is same as that of previous(which is the observable) if both are same then do not update or else update. if(compareArrObjects) this. values = givenobject }
Voulez-vous avec Lodash ou sans Lodash?
avec le lodash