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

121
Vistas
how to find certain key by its value into mixed objects array?
const items = [ { 'first': [205, 208, 222] }, { 'second': 205 } ]

I need to find keys by all coincidence into its value/values

example:

find key by id=205, output: 'first', 'second'

find key by id=208, output: 'first'

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

0

You can try getting the arrays of keys and values, then filter the keys and check if the corresponding index in values contain the variable id.

const items = [ { 'first': [205, 208, 222] }, { 'second': 205 } ]
const id = 205;
let keys = items.map(item => Object.keys(item)[0]);
let values = items.map(item => Object.values(item)[0]);

let output = keys.filter((_, index) => (values[index] == id || (typeof(values[index]) == 'object' && values[index].includes(id))));
console.log(output)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can click on 'Run code snippet' and see the output as mentioned in the question

const items = [{
    first: [205, 208, 222],
  },
  {
    second: 205,
  },
];

const toFind = 205;

const fin = items.reduce((finalOutput, item) => {
  let key = Object.keys(item)[0];
  if (Array.isArray(item[key])) {
    if (item[key].includes(toFind)) finalOutput.push(key);
  } else if (item[key] === toFind) {
    finalOutput.push(key);
  }
  return finalOutput;
}, []);

console.log(fin);

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do something like this

const items = [ { 'first': [205, 208, 222] }, { 'second': 205 } ]


const findKey = (data, value) => data.reduce((res, item) => {
  let [[key, v]] = Object.entries(item)
  v = Array.isArray(v)?v: [v]
  if(v.includes(value)){
    return [...res, key]
  }
  return res
}, [])

console.log(findKey(items, 205))
console.log(findKey(items, 208))

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