0
votes

Comment comparer la météo Deux gares d'objets sont égaux ou non à l'aide de Lodash

J'ai un tableau d'objet qui ressemble à, xxx pré>

maintenant, j'ai un observable code> qui est p> xxx pré >

MAINTENANT, CLIQUEZ Cliquez sur une fonction TRGGERD qui assignera ce tableau d'objet à l'observable. 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

}


2 commentaires

Voulez-vous avec Lodash ou sans Lodash?


avec le lodash


3 Réponses :



0
votes

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));


0 commentaires

0
votes

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
 }


0 commentaires