Kishor Garrix
Kishor Garrix

Reputation: 5

How to form array of arrays in typescript?

I have two arrays,

arr1 = [1589135400000, 1589221800000, 1589308200000, 1589394600000, 1589481000000, 1589567400000, 1589653800000, 1589740200000, 1589826600000, 1591122600000]'    
arr2 = [90.05, 98.0225, 99.8231, 99.3969, 73.45, 68.356, 55.123, 100, 68.75, 98.02430  ]  

i need to form array of arrays which should look like this.

[
      [1589135400000, 90.05],
      [1589221800000, 98.0225],
      [1589308200000, 99.8231],
      [1589394600000, 99.3969],
      [1589481000000, 73.45],
      [1589567400000, 68.356],
      [1589653800000, 55.123],
      [1589740200000, 100],
      [1589826600000, 68.75],
      [1591122600000, 98.02430]

     
]

I am using typescript. please some one help me to solve this.please please Thanks in advance

Upvotes: 0

Views: 229

Answers (2)

MauriceNino
MauriceNino

Reputation: 6757

Just loop over the first and find the same index in the second

arr1 = [1589135400000, 1589221800000, 1589308200000, 1589394600000, 1589481000000, 1589567400000, 1589653800000, 1589740200000, 1589826600000, 1591122600000]
arr2 = [90.05, 98.0225, 99.8231, 99.3969, 73.45, 68.356, 55.123, 100, 68.75, 98.02430 ]

console.log(arr1.map((el, i) => [ el, arr2[i] ]))

Upvotes: 0

Reyno
Reyno

Reputation: 6525

You can use array.map to combine the array values at the same index.

const arr1 = [
  1589135400000,
  1589221800000,
  1589308200000,
  1589394600000,
  1589481000000,
  1589567400000,
  1589653800000,
  1589740200000,
  1589826600000,
  1591122600000,
];

const arr2 = [
  90.05,
  98.0225,
  99.8231,
  99.3969,
  73.45,
  68.356,
  55.123,
  100,
  68.75,
  98.02430,
];

const combined = arr1.map((n, i) => [n, arr2[i]]);

console.log(combined);

Upvotes: 3

Related Questions