-1

I need to construct an object with all value from same position

const arr = [
  [ 0, 2, .. ],
  [ 19, 4, .. ],
  [ 98, 12, .. ]
]

==>

const result = {
  0: [0, 19, 98],
  1: [2, 4, 12],
  ...
}

Thanks

blackratio
  • 19
  • 2

1 Answers1

0

It's like some sort of transposition. A simple nested loop will do.

const arr = [
  [0, 2],
  [19, 4],
  [98, 12]
]

var obj = {}
for (var i = 0; i < arr.length; i++) {
  for (var j = 0; j < arr[i].length; j++) {
    obj[j] = obj[j] || []
    obj[j].push(arr[i][j])
  }
}

console.log(obj)
IT goldman
  • 14,885
  • 2
  • 14
  • 28