Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

172
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda