-2

I have 2 objects that will always have the same amount of properties just in occasion the values may be different.

I am attempting to do a check against the 2 objects to see if they are the same value

I decided to try and handle this using the .every method but all results are returning false even when I am expecting it to be true.

How can I compare 2 objects to see they are equal?

Here is a code snippet

const priceOne = {
"price": 14.31,
"taxes": 2.00,
"total": 16.31
}

const priceTwo = {
"price": 14.31,
"taxes": 2.00,
"total": 16.31
}

const comparePrice = Object.keys(priceOne).every((item, index) => {
      item === Object.keys(priceTwo)[index] ?  true :  false
    })
    
console.log(comparePrice)
eneooo
  • 368
  • 3
  • 11
  • 2
    Already answered here [How to determine equality for two JavaScript objects?](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) – Samathingamajig Aug 17 '22 at 22:07
  • Do you expect `{ a: 1, b: 2 }` and `{ b: 2, a: 1 }` to be equal? – jabaa Aug 17 '22 at 22:13
  • @jabaa I expect both objects being compared to have the same order – eneooo Aug 17 '22 at 22:14
  • That means `{ "price": 14.31, "taxes": 2.00, "total": 16.31 }` and `{ "price": 14.31, "total": 16.31, "taxes": 2.00 }` are different? I'm not sure you, can achieve this running on engines before ES5. AFAIK, the property is defined since ES5. – jabaa Aug 17 '22 at 22:16
  • @jabaa my apologies, I don't follow. I expect them to be in the same order so price, taxes, total comparing to prices, taxes, total. The key order would remain the same but sometimes the content will be different. Example `"price": 14.31` vs `"price": 12.00` etc – eneooo Aug 17 '22 at 22:18
  • The answer to _"How can I compare 2 objects to see they are equal?"_ depends on whether `{ "price": 14.31, "taxes": 2.00, "total": 16.31 }` and `{ "price": 14.31, "total": 16.31, "taxes": 2.00 }` are equal or different. The given answer considers these two objects as different. – jabaa Aug 17 '22 at 22:20
  • @jabaa still confused about the different or equal. They will be 2 separate objects compared together. Not sure if that helps – eneooo Aug 17 '22 at 22:24
  • Why is the question still open without an accepted answer? What's wrong with the given answer and does the duplicate answer your question? – jabaa Aug 17 '22 at 22:27

2 Answers2

0

I would JSON.stringify() and compare that way .. IE:

if ( JSON.stringify(priceOne) === JSON.stringify(priceTwo) ){ 
Zak
  • 6,976
  • 2
  • 26
  • 48
  • this doesn't work if the keys are in a different order, also takes a lot of extra memory and time by stringifying – Samathingamajig Aug 17 '22 at 22:08
  • @Samathingamajig, Correct .. But in my eyes .. Those objects would not be "equal" -- They would contain equal "parts" -- But they are not the "same" – Zak Aug 17 '22 at 22:09
  • also stringifying doesn't work (throws an error) if there is any referential loops – Samathingamajig Aug 17 '22 at 22:10
0

When objects are different:

     const priceOne = {
            "price": 14.31,
            "taxes": 2.00,
            "total": 16.31
      }

      const priceTwo = {
            "price": 12.31,
            "taxes": 2.00,
            "total": 16.31
        }

let comparePrice = true

 for (let key in priceOne){
    if(priceOne[key] != priceTwo[key]){
       comparePrice = false
     }`
  }`

the variable will be false when objects are the same :

const priceOne = {
       "price": 14.31,
       "taxes": 2.00,
       "total": 16.31
 }

const priceTwo = {
      "price": 14.31,
      "taxes": 2.00,
      "total": 16.31
}

the result will be true.

I hope I helped

Instead of string or number is array we can easier add extra validation

 const priceOne = { a: '0' };

 const priceTwo = { a: [0] };

 let comparePrice = true

for (let key in priceOne) {

if(Array.isArray(priceTwo[key] )){
    if (priceOne[key] != priceTwo[key].toString()) {
    comparePrice = false
  }
}else if(Array.isArray(priceOne[key] )) {
    if (priceOne[key].toString() != priceTwo[key]) {
    comparePrice = false
  }
}
if (priceOne[key] != priceTwo[key]) {
    comparePrice = false
   }
}

console.log(comparePrice);
ersati
  • 85
  • 4