Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

57
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda