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

How to format an array as labels and values in JavaScript?

I’m still new to Angular and I’m trying to get a chart for my data using FusionCharts.
FusionCharts uses the following format to get the data.

const chartData = [{
      label: "2021-09-26T00:01:00",
      value: "250"
    },
    {
      label: "2021-09-26T00:02:00",
      value: "251"
    },
    {
      label: "2021-09-26T00:03:00",
      value: "245"
    },
    {
      label: "2021-09-26T00:04:00",
      value: "248"
    },
    {
      label: "2021-09-26T00:05:00",
      value: "251"
    },];

I got two separate arrays as values and labels. How can I convert them to this format.

I tried using dictionaries, but it has {label: value} format that can’t be used with FusionCharts

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 :

That should just be a simple map() operation:

const labels = ['2021-09-26T00:01:00', '2021-09-26T00:02:00'];
const values = ['250', '251'];

const result = labels.map((label, i) => ({ label, value: values[i] }));

console.log(result);
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