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

188
Views
Iterar sobre un objeto y encontrar la clave

Digamos que tengo este conjunto de datos como

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

Ahora quiero encontrar la clave que contiene el número "3435". Para esto, no pude encontrar una forma de iterar sobre los objetos. ¿Hay alguna forma de encontrar una coincidencia sin usar la iteración?

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

0

Tal vez puedas iterar así. No estoy seguro si podemos lograr esto sin iteración.

 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

Una posible solución sería:

 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

No puede iterar directamente sobre un JSON, pero puede obtener la lista de claves con Object.keys() .

A partir de ahí, quedan dos opciones:

Si sabe que solo hay una coincidencia de valor, use un bucle para devolver el seleccionado

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

devuelve 16 .

Si puede haber más de una entrada, use filter()

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

devuelve [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!