Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!