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

196
Views
Iterate over an object and find the key

Let's say I have this data set as

let data = {
  1: ['item1', '3435'],
  32: ['item2', '5465'],
  16: ['item3', '6577']
}

Now I want to find the key which contains the number "3435". For this, I could not find out a way to iterate over objects. Is there any way to find a match without using iteration?

findKey(3534) // should return "1"
findKey(6577) // should return "16"
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Maybe you can iterate like this. Not sure if we can achieve this without iteration.

const getKey = (matchString) => {
  let data = {
    1: ['item1', '3435'],
    32: ['item2', '5465'],
    16: ['item3', '6577']
  }

  for (let item in data) {
    if (data[item].includes(matchString)) {
      return item;
    }
  }
}

const key = getKey('3435')
console.log(key)

about 4 years ago · Juan Pablo Isaza Report

0

A possible solution would be:

 let data = {
  1 : ['item1', '3435'],
  32 : ['item2', '5465'],
  16 : ['item3', '6577']
}

for (const [key, value] of Object.entries(data)) {  
  if(value.includes('3435'))  
  {
    console.log(key)
  }
}

about 4 years ago · Juan Pablo Isaza Report

0

You can't iterate directly over a JSON, but you can get the list of keys with Object.keys().

From there, they are two choices:

If you know there is only one value matching use a loop to return the selected one

function findInObject(value) {
  for (let i in Object.keys(data)) {
    if (data[i][1] == value) {
      return i
    }
  }
}

returns 16.

If there may be more than one entry use filter()

function findInObject(value) {
  return Object.keys(data.filter(elem => elem[1] == value))
}

returns [16, 42, 1000].

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!