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

136
Views
Obtener elemento del objeto js con clave parcialmente conocida

Digamos que no conoce los números antes de la letra aquí.

 var obj = {'1234 x': 1, '32145 y': 2}

¿Cómo recuperaría el valor 1 de este objeto?

La forma normal de hacerlo sería:

 obj['1234 x'] // 1

Pero, ¿y si solo supieras parte de la clave: 'x' (la parte que viene después del primer espacio)

 var key = '1234 x', known_key = key.split(' ')[0] // 'x'

¿Cómo sería capaz de recuperar este valor?

 obj['% ' + known_key] // 1 // % represents all possible strings
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Usa un filtro.

Recuperar la clave y el valor

 function findObject(obj, known_key) { return Object.fromEntries( Object.entries(obj) .filter(e => e[0].substr(e[0].indexOf(' ') + 1) == known_key ) ) } var object = { '1234 x': 1, '32145 y': 2, '987 y': 3 }, key = 'y' console.log( findObject(object, key) // {'32145 y': 2, '987 y': 3} )

recuperar el valor

 function findValue(obj, known_key) { return Object.entries(obj) .filter(e => e[0].substr(e[0].indexOf(' ') + 1) == known_key ) .map(e => e[1]) } var object = { '1234 x': 1, '32145 y': 2, '987 y': 3 }, key = 'y' console.log( findValue(object, key) // [2, 3] )

about 4 years ago · Juan Pablo Isaza Report

0

Filtre usando extremos con: puede sustituir con incluye para encontrar la cadena en cualquier lugar

 const findPartial = (obj,str) => Object.fromEntries(Object.entries(obj) .filter(([key, val]) => key.endsWith(str))); const obj = { '1234 x': 1, '32145 y': 2 } console.log( findPartial(obj, ' y') )

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!