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

162
Visualizações
Crear una matriz de objetos basada en otra matriz de objetos

Intentando crear una matriz de objetos basada en otra matriz de objetos. Decidí usar un mapa plano y luego reducirlo, pero el problema es cuando tengo más de un objeto en la matriz de resultados: no puedo recopilar varios estados en un objeto. He agregado lo que probé y cuál es el resultado que estoy tratando de lograr.

Lo que tengo

 const data = [ { "timestamp": "2021-08-31T15:29:18Z", "result": [ { "label": "Not Covered", "value": 132 } ] }, { "timestamp": "2021-09-30T15:29:18Z", "result": [ { "label": "Not Covered", "value": 135 } ] }, { "timestamp": "2021-10-31T16:29:18Z", "result": [ { "label": "Not Covered", "value": 135 } ] } ]

lo que necesito conseguir

 [ { "Not Covered":132, "Active":0, "Expiring Soon":0, "timestamp": "2021-08-31T15:29:18Z" }, { "Not Covered":135, "Active":0, "Expiring Soon":0, "timestamp": "2021-09-30T15:29:18Z" }, { "Not Covered":135, "Active":0, "Expiring Soon":0, "timestamp": "2021-10-31T16:29:18Z" } ]

Qué estoy haciendo

 let flattenedResult = data.flatMap(({result,...r}) => result.map(o => ({ ...o,...r}))); const chartData = flattenedResult.reduce((acc, item) => { const {timestamp, value,label} = item; acc.push({timestamp, "Not Covered":"Not Covered"===label?value:0,"Active":"Active"===label?value:0,"Expiring Soon":"Expiring Soon"===label?value:0}); return acc; }, []);

Lo que estoy recibiendo:

 [ { "timestamp": "2021-08-31T15:29:18Z", "Not Covered": 132, "Active": 0, "Expiring Soon": 0 }, { "timestamp": "2021-09-30T15:29:18Z", "Not Covered": 135, "Active": 0, "Expiring Soon": 0 }, { "timestamp": "2021-10-31T16:29:18Z", "Not Covered": 135, "Active": 0, "Expiring Soon": 0 } ]
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

No ha explicado cómo obtener los valores "Activo" y "Expirando pronto", pero estoy seguro de que algo así debería ayudar.

 const data = [ { "timestamp": "2021-08-31T15:29:18Z", "result": [ { "label": "Expiring Soon", "value": 57 }, { "label": "Not Covered", "value": 132 }, { "label": "Active", "value": 42 } ] }, { "timestamp": "2021-09-30T15:29:18Z", "result": [ { "label": "Not Covered", "value": 135 } ] }, { "timestamp": "2021-10-31T16:29:18Z", "result": [ { "label": "Not Covered", "value": 135 }, { "label": "Active", "value": 42 } ] } ]; console.log(data.reduce((acc, {timestamp, result}) => { const datum = result.reduce((acc, {label, value}) => { acc[label] = value; return acc; }, { timestamp, 'Not Covered': 0, Active: 0, 'Expiring Soon': 0 }); return acc.concat(datum); }, []));

about 4 years ago · Juan Pablo Isaza Relatório

0

¿Qué pasa con algo como esto? Luego puede simplemente poner su "Activo" y "ExpringSoon" de acuerdo con su lógica comercial.

 const array = data.map(item => { const results = {} item.result.forEach(({ label, value }) => results[label] = value ) return { Active: 0, ExpiringSoon: 0, timestamp: item.timestamp, ...results } })

Resultado

 [ { Active: 0, ExpiringSoon: 0, timestamp: '2021-08-31T15:29:18Z', 'Not Covered': 132 }, { Active: 0, ExpiringSoon: 0, timestamp: '2021-09-30T15:29:18Z', 'Not Covered': 135 }, { Active: 0, ExpiringSoon: 0, timestamp: '2021-10-31T16:29:18Z', 'Not Covered': 135 } ]
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