-1

I have the following data arrays (a, b and c).

let a = [a1, a2, a3];
let b = [b1, b2, b3];
let c = [c1, c2, c3];

But I need the data as in array d. Is it possible to get the array d by combining a, b and c in javascript? Is there any simple method that achieves it or I've to iterate each array and conditionlly add them?

let d = [
         {x: a1, y: b1, z: c1}, 
         {x: a2, y: b2, z: c2}, 
         {x: a3, y: b3, z: c3}
        ]
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Amrita Stha
  • 2,973
  • 3
  • 25
  • 46

1 Answers1

0

If I understand the question correctly, In your case you are accessing elements of multiple arrays at a specific index position in each iteration. I can't think of any shortcut to handle this. You might need to go the iterative way. Below code could help.

a = ["a1","a2","a3"]
b= ["b1", "b2", "b3"]
c= ["c1", "c2", "c3"]
// Input array
f = [a, b, c];
const length = f.length;
d = Array.from({length}).map((_val, index1) => {
    return Array.from({length}).reduce((acc, _val, index2) => {
        acc[f[index2][index1]] = f[index2][index1];
        return acc;
    }, {})
});
console.log(d);
Nithin Thampi
  • 3,579
  • 1
  • 13
  • 13