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

488
Views
Cómo 'búsqueda inversa' de una propiedad en un objeto en JavaScript

¿Cómo se realiza una 'búsqueda inversa' del atributo de un objeto en JavaScript? Por ejemplo, digamos que tengo un objeto:

 var price = { water: '$1.00', beer: '$30.00', xbox: '$500.00' }

Escribir price.water devolvería '$1.00' . Pero, ¿y si quisiera averiguar el ARTÍCULO por el PRECIO , en lugar del precio por el artículo? Como si pudiera escribir fnToFindItemByPrice('$1.00') , y obtener 'water' . es posible?

Gracias por adelantado.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Puede usar Object.keys y Array.find :

 var price = { water: '$1.00', beer: '$30.00', xbox: '$500.00' } function findItemByPrice(item){ return Object.keys(price).find(e => price[e] == item) } console.log(findItemByPrice('$1.00'))

Si tiene más de una propiedad con el valor, puede usar Array.filter :

 var price = { water: '$1.00', beer: '$30.00', xbox: '$500.00', water2: '$1.00', } function findItemByPrice(item){ return Object.keys(price).filter(e => price[e] == item) } console.log(findItemByPrice('$1.00'))

about 4 years ago · Juan Pablo Isaza Report

0

que ?

 const f_reverse = obj => Object.entries(obj).reduce((r,[k,v])=>(r[v]=k,r),{}) const price = { water : '$1.00' , beer : '$30.00' , xbox : '$500.00' } const fruits = { apples : 'normandy' , pears : 'italy' , bananas : 'gabon' , oranges : 'california' } const priceRev = f_reverse(price) const products = f_reverse(fruits) console.log( priceRev['$1.00'] ) // -> water console.log( products.california ) // -> oranges

En caso de varias claves con el mismo valor:

 const f_reverse = obj => Object.entries(obj).reduce((r,[k,v])=> { if (!r.hasOwnProperty(v)) r[v] = k else if (!Array.isArray(r[v])) r[v] = [ r[v], k ] else r[v].push(k) return r },{}) const price = { water : '$1.00' , beer : '$30.00' , xbox : '$500.00' , tacos : '$1.00' , glub : '$1.00' } const priceRev = f_reverse(price) console.log('$1.00->', priceRev['$1.00']) // ['water','tacos','glub'] console.log('$500.00->', priceRev['$500.00']) // 'xbox'
 .as-console-wrapper {max-height: 100%!important;top:0 }

Para los que siempre prefieren tener una matriz trasera.
(en caso de uso por plan de encadenamiento para usar un operador coalescente nulo )

 const f_reverse = obj => Object.entries(obj).reduce((r,[k,v])=>(r[v]??=[],r[v].push(k),r),{}) const price = { water : '$1.00' , beer : '$30.00' , xbox : '$500.00' , tacos : '$1.00' , glub : '$1.00' } const priceRev = f_reverse(price) console.log('$1.00->', priceRev['$1.00']) // ['water','tacos','glub'] console.log('$500.00->', priceRev['$500.00']) // ['xbox']
 .as-console-wrapper {max-height: 100%!important;top:0 }

about 4 years ago · Juan Pablo Isaza Report

0

Tenga en cuenta que los valores no son únicos, a diferencia de las claves.

 var price = { water: '$1.00', beer: '$30.00', coffee: '$1.00', xbox: '$500.00' }; var one_dollar = []; for (let k in price) { if (price[k] === '$1.00') { one_dollar.push(k); } } console.log(one_dollar);

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!