Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Transform multi-dimensional JavaScript array

I have the following javascript (typescript) array:

[[1262304000000,0.7537], [1262563200000,0.6951], [1262649600000,0.6925]]

How can I transform it to the following using map operator? (Basically second element of the array has been converted to % value. E.g. 0.7537 to 75.37. However, I am not sure how to access the second element of the array)

[[1262304000000,75.37], [1262563200000,69.51], [1262649600000,69.25]]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

const data = [[1262304000000, 0.7537], [1262563200000, 0.6951], [1262649600000, 0.6925]];

const convertedData = data.map(item => {
  const timestamp = item[0];
  const percent = item[1] * 100; // Convert the second item to a percentage
  return [timestamp, percent]; // return both items
});

console.log(convertedData);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading