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
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
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)