[Chartjs]-Convert two lists into a dictionary of X and Y Format

2👍

Someone has already written the Javascript solution, I will describe Python way.

a = [1, 2, 3, 4, 5];
b = [9, 8, 7, 6, 5];

data = []

for x, y in zip(a, b):
    data.append({'x': x, 'y': y})

or shorter and pythonic style:

data = [{'x': x, 'y': y} for x, y in zip(a, b)]

4👍

You can use Array#map to generate such an array.

The second parameter of the map function is the index, you can get the y value by that index.

const features = [1, 2, 3, 4, 5, 6];
const labels = [11, 22, 33, 44, 55, 66];

const coords = features.map((x, i) => ({ x, y: labels[i] }));

console.log(coords);

Leave a comment