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

125
Views
Quiero convertir esta matriz en este objeto.
const movies = [ { name: "Escape from Pretoria", year: 2020 }, { name: "Good Will Hunting", year: 1997 }, { name: "The Matrix", year: 1999 }, { name: "Tarzan", year: 1999 }, { name: "Titanic", year: 1997 }, { name: "The Imitation Game", year: 2014 }, ];

objetar así

 const x = { 1997: ["Good will Hunting", "Titanic"], 1999: ["The Matrix", "Tarazan"], 2014: ["The Imitation Game"], 2020: ["Escape from Pretoria"], };

Usando reduce() u otra forma... ¡gracias!

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

0

 const movies = [ { name: "Escape from Pretoria", year: 2020 }, { name: "Good Will Hunting", year: 1997 }, { name: "The Matrix", year: 1999 }, { name: "Tarzan", year: 1999 }, { name: "Titanic", year: 1997 }, { name: "The Imitation Game", year: 2014 }, ]; let obj={}; movies.forEach(({year, name})=>{ if(obj[year]){ obj[year].push(name) } else { obj[year] = [name] } }) console.log(obj);
about 4 years ago · Juan Pablo Isaza Report

0

Aquí hay una solución usando reduce

 const movies = [ { name: "Escape from Pretoria", year: 2020 }, { name: "Good Will Hunting", year: 1997 }, { name: "The Matrix", year: 1999 }, { name: "Tarzan", year: 1999 }, { name: "Titanic", year: 1997 }, { name: "The Imitation Game", year: 2014 }, ]; const result = movies.reduce((acc, curr) => { if (acc[curr.year]) acc[curr.year].push(curr.name) else acc[curr.year] = [curr.name] return acc }, {}) console.log(result)

about 4 years ago · Juan Pablo Isaza Report

0

Ambas soluciones son excelentes, pero en este caso, forEach método es más rápido.

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!