Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

125
Views
Deeply equal function error in Javascript

I am trying to create a function to check whether two arrays are deeply equal to each other. An example would be: [1, 2, { a: "hello" }] and [1, 2, { a: "bye" }] would return false.

This is my code so far:

const deeplyEquals = (val1, val2) => {
  let counter = 0;

  for (var i = 0; i < val1.length; i++) {
    if (typeof val1[i] === "object") {
      deeplyEquals(JSON.stringify(val1[i]), JSON.stringify(val2[i]));
    } else if (typeof val2[i] === "object") {
      deeplyEquals(JSON.stringify(val1[i]), JSON.stringify(val2[i]));
    } else if (val1[i] !== val2[i]) {
      counter++;
    }
  }

  return counter === 0 ? true : false;
};

I implemented a counter so that if it found a value in 1 that was not equal to the same value in 2 then it would increment. If the counter was not 0 then it would return false.

For the example, the counter increments to 7 but then right at the end, changes to 0 and therefore returns true instead of false. I'm sure there would be an easier way to do this but I was wanting to see whether I could make this work as I am unsure why the counter is changing to 0 right at the end.

Thanks for any help!

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The problem is that the counter is local to each call to deeplyEquals. There's a different counter for each call, and since you're making the calls recursively, you have lots of different counter variables in memory at the same time.

If you wanted to maintain a counter, you'd have to have each recursive call return the counter value (instead of a flag) so the code calling it could increment its counter by that much.

But there's no need in your code. Instead, just return false the first time you find a difference, either during in the call itself or in one of its recursive calls by checking the return value of the recursive call.


There are other issues with the code. Here's what I notice off-the-cuff:

  • You're calling JSON.stringify, which returns a string, before passing values to deeplyEquals, which will convert arrays and objects to strings. Comparing the strings won't be reliable (because equivalent objects can have their properties in different orders: JSON.stringify({a:1,b:2}) is the string {"a":1,"b":2}, but JSON.stringify({b:2,a:1}) is the string {"b":2,"a":1}). Instead, pass the actual value.
  • typeof x returns "object" for arrays and null as well as non-array objects; you need to handle those three cases separately.
  • When comparing non-array objects, you need to loop through their properties to compare them.

SO has several questions and answers about doing deep equality checks; probably best to search for those, study them to ensure you understand how they work, and go from there.

about 4 years ago · Juan Pablo Isaza Report

0

Why dont you just JSON stringify both arrays and then compare?


const deepEquals(val1, val2){
    let v1 = JSON.stringify(val1);
    let v2 = JSON.stringify(val2);

    return v1 === v2;
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!