-1

I have the following sample data:

   let abc =   [
      {
        endPointScrape: 'https://www.example.com/computer/component/keyboards-and-mice/microsoft-surface-pen-silver/p/100008669',
        mainCatagoryScrape: 'Computer',
        subCatagoryScrape: 'Component',
        subSubCatagoryScrape: 'Keyboards and mice',
        itemsDataScrape: {
          itemName: 'Microsoft Surface Pen, Silver',
          itemOrignalPrice: '399',
          itemCurrentPrice: '299',
          itemDiscount: '100'
        }
      },
      {
        endPointScrape: 'https://www.example.com/computer/component/keyboards-and-mice/microsoft-surface-arc-mouse-sc-bluetooth-xz-ar-hdwr-light-grey/p/100008664',
        mainCatagoryScrape: 'Computer',
        subCatagoryScrape: 'Component',
        subSubCatagoryScrape: 'Keyboards and mice',
        itemsDataScrape: {
          itemName: 'Microsoft Surface Arc Mouse SC Bluetooth XZ/AR Hdwr, Light Grey',
          itemOrignalPrice: '319',
          itemCurrentPrice: '239',
          itemDiscount: '80'
        }
      },
      {
        endPointScrape: 'https://www.example.com/white-goods/washing-machines/topload/classpro-top-load-fully-automatic-washing-machine-12kg-white/p/100055634',
        mainCatagoryScrape: 'White goods',
        subCatagoryScrape: 'Washing machines',
        subSubCatagoryScrape: 'Topload',
        itemsDataScrape: {
          itemName: 'ClassPro Top Load Fully Automatic Washing Machine, 12kg, White',
          itemOrignalPrice: '1849',
          itemCurrentPrice: '1349',
          itemDiscount: '500'
        }
      },
      {
        endPointScrape: 'https://www.example.com/white-goods/washing-machines/topload/classpro-top-load-fully-automatic-washing-machine-9kg-white/p/100055635',
        mainCatagoryScrape: 'White goods',
        subCatagoryScrape: 'Washing machines',
        subSubCatagoryScrape: 'Topload',
        itemsDataScrape: {
          itemName: 'ClassPro Top Load Fully Automatic Washing Machine, 9kg, White',
          itemOrignalPrice: '1499',
          itemCurrentPrice: '1099',
          itemDiscount: '400'
        }
      }
      ]

Now the above array of objects i want to sort them within an array if they have the same mainCatagoryScrape, subCatagoryScrape and subSubCatagoryScrape. So as my output looks somewhat like below as the end result.

enpointScrape as an array of strings and itemdatascrape as an array of objects (respectively) that contains items with same maincatagory,subcatagory and subsubcatagory.

let abc = [
      {
        endPointScrape: [
'https://www.example.com/computer/component/keyboards-and-mice/microsoft-surface-pen-silver/p/100008669',
'https://www.example.com/computer/component/keyboards-and-mice/microsoft-surface-arc-mouse-sc-bluetooth-xz-ar-hdwr-light-grey/p/100008664'],
        mainCatagoryScrape: 'Computer',
        subCatagoryScrape: 'Component',
        subSubCatagoryScrape: 'Keyboards and mice',
        itemsDataScrape: [{
          itemName: 'Microsoft Surface Pen, Silver',
          itemOrignalPrice: '399',
          itemCurrentPrice: '299',
          itemDiscount: '100'
        },
        {
          itemName: 'Microsoft Surface Arc Mouse SC Bluetooth XZ/AR Hdwr, Light Grey',
          itemOrignalPrice: '319',
          itemCurrentPrice: '239',
          itemDiscount: '80'
        }]
      },
  {
        endPointScrape: [
'https://www.example.com/white-goods/washing-machines/topload/classpro-top-load-fully-automatic-washing-machine-12kg-white/p/100055634',
'https://www.example.com/white-goods/washing-machines/topload/classpro-top-load-fully-automatic-washing-machine-9kg-white/p/100055635'],
        mainCatagoryScrape: 'White goods',
        subCatagoryScrape: 'Washing machines',
        subSubCatagoryScrape: 'Topload',
        itemsDataScrape: [{
          itemName: 'ClassPro Top Load Fully Automatic Washing Machine, 12kg, White',
          itemOrignalPrice: '1849',
          itemCurrentPrice: '1349',
          itemDiscount: '500'
        },
        {
          itemName: 'ClassPro Top Load Fully Automatic Washing Machine, 9kg, White',
          itemOrignalPrice: '1499',
          itemCurrentPrice: '1099',
          itemDiscount: '400'
        }]
      }]

i dont have access to all the catagories and they differ. hence i want to sort the same catagory items by creating a new array which matches with other object values and create unique maincategory,subcategory and subsubcatagory object with arrays of all those with same values. i tried few solutions but nothing came out to be definitive.

XenaD Aux
  • 25
  • 5
  • Does this answer your question? [How to group an array of objects by key](https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key) – PaulH Sep 17 '20 at 02:21

1 Answers1

0

You could do a groupBy like this:

let abc = [{ your data }, ... ];
let r = {};
abc.forEach(function(a) {
  // create grouping key
  let key = a.mainCatagoryScrape + a.subCatagoryScrape + a.subSubCatagoryScrape;

  // create group if not exist
  r[key] = r[key] || {
    mainCatagoryScrape: a.mainCatagoryScrape,
    subCatagoryScrape: a.subCatagoryScrape,
    subSubCatagoryScrape: a.subSubCatagoryScrape,
    endPointScrapes: [],
    itemsDataScrapes: []
  };

  // add individual record data
  r[key].endPointScrapes.push(a.endPointScrape);
  r[key].itemsDataScrapes.push(a.itemsDataScrape);
});

// remove keys (keep only values) and output
console.log(Object.values(r));
PaulH
  • 2,918
  • 2
  • 15
  • 31
  • thank you so much. that works like a charm. with small edits so that the itemdata push is r[key].itemdatascrapes.push({itemname: a.itemDataScrape.itemName, ..}). Although i want to output r as an array of objects that contains all the other objects, currently i get r as an object with keys as the filters for it. Any suggestions there , maybe push all the keys to a seperate array with objects that contains the keys as well. – XenaD Aux Sep 17 '20 at 13:47
  • edited the code to match the solution of sorts. now the issue is to have r as an array rather than an object. so as the corresponding {key:anythinganythinganything} is added to each object that is pushed to a new array of objects. – XenaD Aux Sep 17 '20 at 13:58