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

171
Vistas
How to find item in object by value in JS

There are many questions about this, but none of them solved my problem.

I have this object:

const default_keys = {
    l: 1, 
    t: 5,
    o: 10, 
    r: 50, 
    w: 100,
    y: 500,
    m: 1000,
    f: 5000,
    a: 10000,
    z: 50000,
    d: 100000,
    q: 1000000
};

so, among all these key:value items, I want to know which key has the value = 100 (in this case it should return "w");

is it possible?

EDIT: I tried this solution, but it didn't work because it only works with fixed key names:

const object1 = {
  a: {val: "aa"},
  b: {val: "bb"},
  c: {val: "cc"}
};

let a = Object.values(object1).find((obj) => {
    return obj.val == "bb"
});

in this case the fixed name is "val", but in my case there is no fixed name..

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

One way would be to iterate thru Object.entries.

const default_keys = {
    l: 1, 
    t: 5,
    o: 10, 
    r: 50, 
    w: 100,
    y: 500,
    m: 1000,
    f: 5000,
    a: 10000,
    z: 50000,
    d: 100000,
    q: 1000000
};

function getKey(object, value){
  for (const [k, v] of Object.entries(object)) {
    if(v == value) return k;
  }
}

console.log(getKey(default_keys, 100));

Another way would be to use find.

const default_keys = {
  l: 1,
  t: 5,
  o: 10,
  r: 50,
  w: 100,
  y: 500,
  m: 1000,
  f: 5000,
  a: 10000,
  z: 50000,
  d: 100000,
  q: 1000000
};

function getKey(object, value) {
  let entry = Object.entries(object).find(item => item[1] == value);
  if (entry) return entry[0];
}

console.log(getKey(default_keys, 100));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Using for...in:

const getKeyWithValue = (obj = {}, value) => {
  let key;
  for(const prop in default_keys) {
    if(default_keys[prop] === value) {
      key = prop;
      break;
    }
  }
  return key;
}

const default_keys = { l: 1, t: 5, o: 10,  r: 50,  w: 100, y: 500, m: 1000, f: 5000, a: 10000, z: 50000, d: 100000, q: 1000000 };
console.log( getKeyWithValue(default_keys, 100) );

about 4 years ago · Juan Pablo Isaza Denunciar

0

You could take an object with switched key/value pairs and get the key by taking the value as key.

const 
    keys = { l: 1,  t: 5, o: 10,  r: 50,  w: 100, y: 500, m: 1000, f: 5000, a: 10000, z: 50000, d: 100000, q: 1000000 },
    values = Object.fromEntries(Object.entries(keys).map(([k, v]) => [v, k]));

console.log(values[100]);

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