Chartjs-How can I pass the data into the chartjs label?

1👍

You can use Set,
its avoid duplicate values:

let ar = new Set();
  data.map((item) => {
    ar.add(item.items.selectedItem);
  });

ar = Array.from(ar); //Convert Set to Array

EDIT:
OK, for that I think its better to use:

let ar = {};
  for(let i=0; i<data.length; i++){
    if(ar[data[i].items.selectedItem] === undefined)
      ar[data[i].items.selectedItem] = 1;
    else
      ar[data[i].items.selectedItem]++;
  }

  let data_labels = Object.keys(ar);
  let data_ = Object.keys(ar).map(key => ar[key]);

And replace:

data={{
          labels: data_labels,
          datasets: [
            {
              data: data_,
              backgroundColor: ["red", "yellow", "green", "blue", "pink"],
              borderColor: ["rgba(255, 99, 132, 1)"],
              borderWidth: 1
            }
          ]
        }}

And for the backgroundColor array You need to check the length of ar, and make an array of this size with different colors if you want.

Leave a comment