0

From service I got below response:

 "rows": [
    [
      "stravi/aa",
      "202001",
      "59",
      "51",
      "2558.98",
      "0.5358894453719162",
      "1.9204668112983725",
      "140",
      "2.3466309084813943"
    ],
    [
      "stravi/ab",
      "202003",
      "3591",
      "349",
      "2246.09",
      "0.41838214",
      "3.57603358",
      "50",
      "4.82115474"
    ],
    [
      "stravi/ac",
      "202007",
      "3354",
      "25",
      "1975.76",
      "0.74220667708",
      "1.12321555541",
      "11",
      "0.9324532454"
    ]
  ]

I would like to split below nested arrays to 9 arrays - when in 1 array will be 1 row from each of array, in 2 array -> second row etc. =>

["stravi/aa", "stravi/ab", "stravi/ac"]
[202001", "202003","202006"]

etc.

I am trying to it by using splitted, map, filter... But still I got mess...

mbojko
  • 13,503
  • 1
  • 16
  • 26

1 Answers1

0

this snippet will do the job; the result will be an array of arrays you are looking for; it could be written in more complex way using reduce() but I prefer more readable version:

let result = [];
rows.forEach( row => {
  row.forEach( (item,index) => {
    if(result[index]){
      result[index].push(item)
    }else{
      result[index] = [item]
    }
  })
})

let  rows = [
    [
      "stravi/aa",
      "202001",
      "59",
      "51",
      "2558.98",
      "0.5358894453719162",
      "1.9204668112983725",
      "140",
      "2.3466309084813943"
    ],
    [
      "stravi/ab",
      "202003",
      "3591",
      "349",
      "2246.09",
      "0.41838214",
      "3.57603358",
      "50",
      "4.82115474"
    ],
    [
      "stravi/ac",
      "202007",
      "3354",
      "25",
      "1975.76",
      "0.74220667708",
      "1.12321555541",
      "11",
      "0.9324532454"
    ]
  ];

let result = [];
rows.forEach( row => {
  row.forEach( (item,index) => {
    if(result[index]){
      result[index].push(item)
    }else{
      result[index] = [item]
    }
  })
})
console.log(result)
Mechanic
  • 5,015
  • 4
  • 15
  • 38