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

59
Views
Javascript comparing at least one identical pairs in an object

I have two dictionaries (object in JS?), and I want to compare them.

In a while loop, I want to print True if at least one of the pairs is identical (i.e. the dictionary below), as opposed to the whole dictionary being identical (but if the whole dict is identical the statement must still be True obviously):

my_dict = {"Text1":"Text1", "Text2":"Text3", "text5":"text5"}

I know in python it would be like that:

while any(key == value for key, value in my_dict.items()):
    ...
else:
    ...

But I can’t get my hands on the correct syntax for JavaScript.

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

0

With Object.entries() and some() this is a one-liner:

let my_dict = {"Text1":"Text1", "Text2":"Text3", "text5":"text5"}

let answer = Object.entries(my_dict).some(([key,value]) => key == value);

console.log(answer)

about 4 years ago · Juan Pablo Isaza Report

0

for(var key in obj){
    if(key === obj[key]){
        //Do Something
    } else {
        //Do Something Else
    }
}

That iterates through the object, and if the key is equal to the value, then it does something, otherwise it does something else.

about 4 years ago · Juan Pablo Isaza Report

0

You can use the some() function on JavaScript arrays which is identical to any from Python I think.

let my_dict = {"Text1":"Text1", "Text2":"Text3", "text5":"text5"}

let same_exists = Object.keys(my_dict).some((key) => key === my_dict[key])
console.log(same_exists)

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!