Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
Merge data using JavaScript

I would like to merge performance data using portfolio as base.

const data = {"portfolio": {"name": "portfolio 1","performance": [{"date": "2022-01-01","value": 100},{"date": "2022-01-15","value": 150}, {"date": "2022-02-01","value": 200}],"funds": [{"name": "fund 1","performance": [{"date": "2022-01-01","value": 3}, {"date": "2022-02-01","value": 4}],},{"name": "fund 2","performance": [{"date": "2022-01-01","value": 5}, {"date": "2022-02-01","value": 6}, {"date": "2022-03-01","value": 7}]}]}};

const {name, performance, funds} = data.portfolio;
const result = {
    headers: ["date", name, ... funds.map(({name}) => name)],
    data: funds[0].performance.map(({date, value}, i) => 
        [date, performance[i].value, ...funds.map(({performance: {[i]: {value}}}) => value)]
    )
};

console.log(result);

Current result:

{
  data: [["2022-01-01", 100, 3, 5], ["2022-02-01", 150, 4, 6]],
  headers: ["date", "portfolio 1", "fund 1", "fund 2"]
}

Expected result:

{
  headers: ["date", "portfolio 1", "fund 1", "fund 2"],
  data: [["2022-01-01", 100,3, 5], ["2020-01-15", 150, , ], ["2022-02-01", 200, 4, 6]]
}

Note that it does not include "2022-03-01" as portfolio does not have it.

JSFiddle: https://jsfiddle.net/mkdeveloper2021/o30nbf6j/9/

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

As in this structure the performance arrays per fund do not necessarily have the same length as the global performance arrays, the matching entries do not occur at the same index, and must be explicitly matched by date.

In other words, the part that destructures a parameter with {[i]: {value}} needs to be replaced by a different solution. You'll have to find the right index. I suggest to use find for that:

const data = {"portfolio": {"name": "portfolio 1","performance": [{"date": "2022-01-01","value": 100}, {"date": "2022-01-15","value": 150}, {"date": "2022-02-01","value": 200}], "funds": [{"name": "fund 1","performance": [{"date": "2022-01-01","value": 3}, {"date": "2022-02-01","value": 4}],},{"name": "fund 2","performance": [{"date": "2022-01-01","value": 5}, {"date": "2022-02-01","value": 6}],}]}};

const {name, performance, funds} = data.portfolio;
const result = {
    headers: ["date", name, ...funds.map(({name}) => name)],
    data: performance.map(({date, value}, i) => 
        [date, value, ...funds.map(({performance}) => 
            performance.find(item => item.date == date)?.value
        )]
    )
};

console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda