[Chartjs]-Display two datasets from an array using chart.js in node.js

1👍

you can make a function to add and remove the duplicates items before you are taking out the labels and datas separately from the rows array. It will add the duplicates values and make only one key pair in the object which you are getting from AJAX call.

const addDuplicate = () => {
  const rows = [
    { labels: 'Avocados', data: 100 },
    { labels: 'Maize', data: 25000 },
    { labels: 'Tomatoes', data: 50 },
    { labels: 'Tomatoes', data: 50 },
    { labels: 'Cabbages', data: 4000 },
    { labels: 'French Beans', data: 5500 },
    { labels: 'Avocados', data: 50000 },
    { labels: 'Avocados', data: 34000 },
    { labels: 'Tomatoes', data: 50 }
  ];

  for (let i = 0; i < rows.length; i++) {
    for (let j = i + 1; j < rows.length; j++) {
      if (rows[i].labels === rows[j].labels) {
        rows[i].data += rows[j].data;
        rows.splice(j, 1);
        j--;
      }
    }
  }
  console.log(rows);
};


Leave a comment