How could i change my JSON structure into chartJS barchart JSON structure?

1👍

While the question is quite unclear regarding the intended usage of the target format, the actual transformation for the example can easily be achieved.

If we assume all input elements share the same data properties (Jul * in this case), we can extract the labels from an arbitrary input element.
The data sets can be obtained by mapping the input data.

// a filter function to determine the data properties we're interested in
let dataPropertiesFilter = (k) =>  k !== "flight" && k !== "range";

let result = {
  // this assumes the first element has all data properties set and subsequent ones share the same properties
  labels: Object.keys(input[0]).filter(dataPropertiesFilter),

  // transforms each input element into the target format
  datasets: input.map(e => {
    return {
      label: e.flight,
      data: Object.keys(e)
        .filter(dataPropertiesFilter)
        .map(v => e[v])
    };
  })
};

Here’s a codesandbox for demonstration: https://codesandbox.io/s/stack-overflow-q-62406854-vzxnq?file=/src/index.js

Leave a comment