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

99
Views
Agrupar objetos si los valores de propiedad son los mismos

Estoy tratando de agrupar los objetos para los usuarios que tienen la misma location y el mismo valor de type que se muestra en el siguiente ejemplo. ¿Cuál sería la mejor manera de abordar esto?

 const diabetics = [ { type: 'type 1', location: 'Boston' email: 'person1@gmail.com' }, { type: 'type 2', location: 'New York' email: 'person2@gmail.com' }, { type: 'type 1', location: 'Boston' email: 'person3@gmail.com' }, { type: 'type 1', location: 'Maine' email: 'person4@gmail.com' }, ] // expected output const diabetics = [ { type: 'type 1', location: 'Boston' email: [ 'person1@gmail.com', 'person3@gmail.com' ] }, { type: 'type 2', location: 'New York' email: 'person2@gmail.com' }, { type: 'type 1', location: 'Maine' email: 'person4@gmail.com' }, ]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

También puede obtener el resultado usando Array.reduce() , creando grupos usando una clave compuesta por type y location .

Una vez que tenemos un mapa ingresado en tipo y ubicación, podemos usar Array.values() para devolver el resultado deseado:

 const diabetics = [ { type: 'type 1', location: 'Boston', email: 'person1@gmail.com' }, { type: 'type 2', location: 'New York', email: 'person2@gmail.com' }, { type: 'type 1', location: 'Boston', email: 'person3@gmail.com' }, { type: 'type 1', location: 'Maine', email: 'person4@gmail.com' }, ] const result = Object.values(diabetics.reduce ((acc, { type, location, email }) => { // Create a grouping key, in this case using type and location... const key = [type, location].join("-"); acc[key] ||= { type, location, email: [] }; acc[key].email.push(email); return acc; }, {})) console.log('Result:', result);

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!