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

202
Views
Matriz de objetos -> filtrar claves o valores por comienza con -> fusionar el resultado con el objeto

Tengo una serie de objetos:

 let cars = [ { "color_1": "purple", "color_2": "red", "type": "minivan", "capacity": 7, "all_colors": [] }, { "color_1": "blue", "color_2": "orange", "type": "station wagon", "capacity": 5, "all_colors": [] } ]

Quiero filtrar todas las claves (comienza con) "color..." y fusionarlas en "all_colors" de esta manera:

 let cars = [ { "color_1": "purple", "color_2": "red", "type": "minivan", "capacity": 7, "all_colors": ['purple', 'red'] }, {...} ]

Intenté esto:

 var res = Object.keys(Object.assign({}, ...cars)).filter(v => v.startsWith('color')); console.log(res)

pero lo necesito para todos los objetos y no sé cómo agregar los resultados en "all_colors"?

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

0

Puedes hacerlo así:

 let cars = [ { "color_1": "purple", "color_2": "red", "type": "minivan" ,"capacity": 7, "all_colors": [] }, { "color_1": "blue", "color_2": "orange", "type": "station wagon","capacity": 5, "all_colors": [] } ]; for (const car of cars) { const color_properties = Object.keys(car).filter((key) => key.startsWith('color')); car.all_colors = color_properties.map((color) => car[color]); } console.log(cars);

about 4 years ago · Juan Pablo Isaza Report

0

Puede obtener entradas, filtrar y mapear los valores.

 const cars = [{ color_1: "purple", color_2: "red", type: "minivan", capacity: 7, all_colors: [] }, { color_1: "blue", color_2: "orange", type: "station wagon", capacity: 5, all_colors: [] }], result = cars.map(o => ({ ...o, all_colors: Object .entries(o) .filter(([k]) => k.startsWith('color')) .map(([, v]) => v) })); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

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!