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

121
Visualizações
how to get the object key if the object and array value matches in javascript

I would like to know how to compare object value and array value in javascript

if the array value and object value is same, then how to return the

object key value in javascript


var result = obj.values(obj).includes(arr2) ? 
'status: active' : 'status: inactive'

var obj = {
  "active": 12,
  "inactive": 14
  "neutral": 16
}

var arr1=[12]
var arr2=[12, 14]
var arr3=[12, 16]

Expected Output

//for arr1
status: active
// for arr2
status: active
status: neutral
// for arr3
status: active
status: inactive

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You should check that the array includes the object value, not vice-versa:

function* matches(object, array) {
  for (const [key, value] of Object.entries(object)) {
    if (array.includes(value))
      yield key;
  }
};

for (const match of matches(obj, arr1))
  console.log(`status: ${match}`);

for (const match of matches(obj, arr2))
  console.log(`status: ${match}`);

for (const match of matches(obj, arr3))
  console.log(`status: ${match}`);

This should work.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can simply iterate through the array, and then attempt to fetch the [key, value] tuples returned by Object.entries(obj) whose value matches the array value. Once found, you return the key in the tuple, i.e.:

arr.map(v => Object.entries(obj).find(x => x[1] === v)[0]);

Note: If you array may contain values that are not present in the object, the code above will throw an error because .find() will return undefined. If that's the case, you need to catch cases where an invalid value is used (defensive design):

arr.map(v => {
  const foundTuple = Object.entries(obj).find(x => x[1] === v);
  return foundTuple ? foundTuple[0] : null;
});

See proof-of-concept below:

const obj = {
  "active": 12,
  "inactive": 14,
  "neutral": 16
}

const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];
const invalid_arr = [12, 999];

function getKeys(obj, arr) {
  return arr.map(v => {
    const foundTuple = Object.entries(obj).find(x => x[1] === v);
    return foundTuple ? foundTuple[0] : null;
  });
}

console.log(getKeys(obj, arr1));
console.log(getKeys(obj, arr2));
console.log(getKeys(obj, arr3));
console.log(getKeys(obj, invalid_arr));

about 4 years ago · Juan Pablo Isaza Relatório

0

Another one solution

const obj = { "active": 12, "inactive": 14, "neutral": 16 }

const arr1 = [12];
const arr2 = [12, 14];
const arr3 = [12, 16];

const getStatuses= (obj, arr) => arr
  .map(arrValue => {
    const [status] = Object.entries(obj)
      .find(([objKey, objValue]) => objValue === arrValue) 
      ?? [null];
    return { status };
  });

console.log(getStatuses(obj, arr1));
console.log(getStatuses(obj, arr2));
console.log(getStatuses(obj, arr3));
.as-console-wrapper{min-height: 100%!important; top: 0}

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