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

121
Views
¿Cómo transformo los datos de esta API en una matriz de objetos organizados por un accesorio específico?

Obtengo estos datos de una API:

 let dataFromApi = [ { // lots of other props name: 'super cool place', address: 'address of food place', classification: 'food', }, { // lots of other props name: 'name of tech place', address: 'address of tech place', classification: 'tech', }, { // lots of other props name: 'name of clothes place', address: 'address of clothes place', classification: 'clothes', }, { // lots of other props name: 'name of of another clothes place', address: 'address of another clothes place', classification: 'clothes', }, { // lots of other props name: 'name of of another tech place', address: 'address of another tech place', classification: 'tech', }, ]

Lo que me gustaría hacer es crear una nueva matriz de objetos que tenga 2 cosas:

  1. Un apoyo llamado classification ;

  2. Un accesorio llamado establishments , que es una matriz de objetos que contiene todos los establecimientos que coinciden con el accesorio de clasificación.


Significa algo como esto:

 let establishmentsArray = [ { classification: 'food', establishments: [ { name: 'name of food place', address: 'address of food place', classification: 'food', // rest of its props }, // Lots of other food places places ], }, { classification: 'clothes', establishments: [ { name: 'name of clothes place', address: 'address of clothes place', classification: 'clothes', // rest of its props }, { name: 'name of another clothes place', address: 'address of another clothes place', classification: 'clothes', // rest of its props }, // Lots of other clothes places ], }, { classification: 'tech', establishments: [ { name: 'name of tech place', address: 'address of tech place', classification: 'tech', // rest of its props }, { name: 'name of another tech place', address: 'address of another tech place', classification: 'tech', // rest of its props }, // Lots of other tech places ], }, ]

¡Gracias por la ayuda por adelantado!

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

0

Parece que está intentando agrupar por campo único, lo cual es un buen caso de uso para array.reduce :

 let dataFromApi = [ { // lots of other props name: 'super cool place', address: 'address of food place', classification: 'food', }, { // lots of other props name: 'name of tech place', address: 'address of tech place', classification: 'tech', }, { // lots of other props name: 'name of clothes place', address: 'address of clothes place', classification: 'clothes', }, { // lots of other props name: 'name of of another clothes place', address: 'address of another clothes place', classification: 'clothes', }, { // lots of other props name: 'name of of another tech place', address: 'address of another tech place', classification: 'tech', }, ] let output = dataFromApi.reduce((acc, cur) => { let { classification, ...rest} = cur; let prev = acc.find(x => x.classification === classification); if(!prev) { acc.push({classification, establishments: [cur]}); } else { prev.establishments.push(cur); } return acc; }, []); console.log(output);

about 4 years ago · Juan Pablo Isaza Report

0

Aquí hay otro ejemplo, puede usar map.reduce :

 const groupByClassification = (items) => { const grouped = items.reduce((acc, cur) => { if (cur.classification in acc) { acc[cur.classification].establishments.push(cur); return acc; } const newGroupItem: GroupedItem = { classification: cur.classification, establishments: [cur] }; return { ...acc, [cur.classification]: newGroupItem }; }, {} }); return Object.values(grouped); };
about 4 years ago · Juan Pablo Isaza Report

0

 let dataFromApi = [ { // lots of other props name: 'super cool place', address: 'address of food place', classification: 'food', }, { // lots of other props name: 'name of tech place', address: 'address of tech place', classification: 'tech', }, { // lots of other props name: 'name of clothes place', address: 'address of clothes place', classification: 'clothes', }, { // lots of other props name: 'name of of another clothes place', address: 'address of another clothes place', classification: 'clothes', }, { // lots of other props name: 'name of of another tech place', address: 'address of another tech place', classification: 'tech', }, ] function GroupByClassification(data){ var groupedData = {}; for (const element of data) { if(!(element.classification in groupedData)){ groupedData[element.classification] = [element]; }else{ groupedData[element.classification].push(element); } } return groupedData; } console.log(GroupByClassification(dataFromApi));

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!