Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

108
Visualizações
Cambiar la estructura de la matriz de JavaScript

Soy un principiante en javascript y tengo un pequeño problema. quiero cambiar la estructura de la matriz para renderizar en React Native usando la sección Lista,

Obtuve este JSON de Web Api

 [ { title: "Test", c: 1, d: 2, }, { title: "Test", c: 3, d: 4, }, { title: "Test", c: 5, d: 6, }, { title: "Test01", c: 1, d: 2, }, { title: "Test01", c: 3, d: 4, }, { title: "Test01", c: 5, d: 6, }, { title: "Test02", c: 1, d: 2, }, { title: "Test02", c: 3, d: 4, }, { title: "Test02", c: 5, d: 6, }, ];

Y quiero cambiar este JSON así

 [ { title: "Test", data: [ { c: 1, d: 2 }, { c: 3, d: 4 }, { c: 5, d: 6 }, ], }, { title: "Test01", data: [ { c: 1, d: 2 }, { c: 3, d: 4 }, { c: 5, d: 6 }, ], }, { title: "Test02", data: [ { c: 1, d: 2 }, { c: 3, d: 4 }, { c: 5, d: 6 }, ], }, ];
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

Sería más sencillo ingresar sus datos en el nombre de la prueba, pero puede lograr lo que desea mapeando su matriz de esta manera:

 let new_array=[]; your_array.forEach(elem => { let title = elem.title; let matchingIndex = newArray.findIndex(a => a.title = title); if (matchingIndex === -1) { new_array.push({title} matchingIndex = new_array.length - 1; } let dataColumns = ['c', 'd']; let data = {}; dataColumns.forEach(col => { data[col] = elem[col]; }); if (!Array.isArray(new_array[matching_index].data)) { isArray(new_array[matching_index].data = []; } new_array[matching_index].data.push(data); });

about 4 years ago · Juan Pablo Isaza Relatório

0

Puede realizar la operación de reducción en la matriz y obtener el formato deseado.

 const items = [ { title: "Test", c: 1, d: 2, }, { title: "Test", c: 3, d: 4, }, { title: "Test", c: 5, d: 6, }, { title: "Test01", c: 1, d: 2, }, { title: "Test01", c: 3, d: 4, }, { title: "Test01", c: 5, d: 6, }, { title: "Test02", c: 1, d: 2, }, { title: "Test02", c: 3, d: 4, }, { title: "Test02", c: 5, d: 6, }, ]; const formatted = items.reduce((carry, current) => { // generating the placeholder format to put the data if(!carry.hasOwnProperty(current.title)) { carry[current.title] = { title: current.title, data: [] }; } // Setting the data in unique name carry[current.title].data.push({ c: current.c, d: current.d }); return carry; }, []); // formatted will have key value pair console.log(Object.values(formatted));

about 4 years ago · Juan Pablo Isaza Relatório

0

Una función de reducción para acumular el resultado en una matriz:

 const raw = [{ title: 'Test', c: 1, d: 2, }, { title: 'Test', c: 3, d: 4, }, { title: 'Test', c: 5, d: 6, }, { title: 'Test01', c: 1, d: 2, }, { title: 'Test01', c: 3, d: 4, }, { title: 'Test01', c: 5, d: 6, }, { title: 'Test02', c: 1, d: 2, }, { title: 'Test02', c: 3, d: 4, }, { title: 'Test02', c: 5, d: 6, }, ]; const result = raw.reduce((acc, { title, ...data }) => { const index = acc.findIndex((elem) => title === elem.title); if (index === -1) acc.push({ title, data: [data] }); else acc[index].data.push(data); return acc; }, []); console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda