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

77
Vistas
Transposing JavaScript Multi D arrays

Give the title:

row.push({"ticker" : PX_Hist[j]['ticker']});

Calcluate data with different timeframe parameters

   const timeframes =  [5,10,90,120,250,500];

for (let t = 0; t <= timeframes.length - 1; t++){
            row.push({"move" : ((PX_Hist[j]['px'][PX_Hist[j]['px'].length-1]["adjusted_close"] / PX_Hist[j]['px'][PX_Hist[j]['px'].length-timeframes[t]]["adjusted_close"] -1))});
        }

I am creating the following output with this code.

[
  [
    {
      "ticker": "JPM"
    },
    {          "move": 0.01405944118170499        },
    {          "move": 0.0337445573294628        },
    {          "move": 0.1692882281117576        },
    {          "move": 0.07636499188035195        },
    {          "move": 0.8151371865267423        },
    {          "move": 0.4537049320855997        }
  ],
  [
    {
      "ticker": "C"
    },
    {          "move": -0.01295986622073586        },
    {          "move": 0.002689694224235595        },
    {          "move": 0.05544868117343582        },
    {          "move": -0.0457495911125243        },
    {          "move": 0.7837535634777528        },
    {          "move": 0.05665004788714745        }
  ],
  [
    {
      "ticker": "C"
    },
    {          "move": -0.01295986622073586        },
    {          "move": 0.002689694224235595        },
    {          "move": 0.05544868117343582        },
    {          "move": -0.0457495911125243        },
    {          "move": 0.7837535634777528        },
    {          "move": 0.05665004788714745        }
  ],
  
]

I need to transpose the above array to something that I can easily bind to a table like below:

    [{"ticker": "JPM", "5": 0.01405944118170499,"10": 0.0337445573294628,"90": 
    0.1692882281117576,"120": 0.07636499188035195,"250": 0.8151371865267423,"500": 
    0.4537049320855997}]

Any words of advice about how to do this in an elegant way?

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

0

You can so something like this, using map and for loop. But honestly I find this unnecessary. You could just do this from the get go in your loop. instead of pushing to row, you could try:

row[0][timeframes[t]] = "that long thing you have there"

const arr = [
  [{
      "ticker": "JPM"
    },
    {
      "move": 0.01405944118170499
    },
    {
      "move": 0.0337445573294628
    },
    {
      "move": 0.1692882281117576
    },
    {
      "move": 0.07636499188035195
    },
    {
      "move": 0.8151371865267423
    },
    {
      "move": 0.4537049320855997
    }
  ],
  [{
      "ticker": "C"
    },
    {
      "move": -0.01295986622073586
    },
    {
      "move": 0.002689694224235595
    },
    {
      "move": 0.05544868117343582
    },
    {
      "move": -0.0457495911125243
    },
    {
      "move": 0.7837535634777528
    },
    {
      "move": 0.05665004788714745
    }
  ],
  [{
      "ticker": "C"
    },
    {
      "move": -0.01295986622073586
    },
    {
      "move": 0.002689694224235595
    },
    {
      "move": 0.05544868117343582
    },
    {
      "move": -0.0457495911125243
    },
    {
      "move": 0.7837535634777528
    },
    {
      "move": 0.05665004788714745
    }
  ],

]

const flatObj = (arr) => {
  const flatObject = {};
  const keys = ["ticker", "5", "10", "90", "120", "250", "500"]
  for (let i = 0; i < arr.length; i++) {
    for (const property in arr[i]) {
      flatObject[keys[i]] = arr[i][property];
    }
  };
  return flatObject;
}

const result = arr.map(ar => flatObj(ar))

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