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?

Answer

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)