Home > Software design >  Need to Group array of JSONs by Product. Display as comma separated products
Need to Group array of JSONs by Product. Display as comma separated products

Time:10-26

i'm new on it and i'm trying from this JSON array in Javascript

So i need to sort the "Name" by Product they were assigned

[
   {
      "Name":"Marcelo Rocha",
      "Product_Line__c":"SAP",
      "Role":"Regional Manager, SAP Support Delivery, LATAM"
   },
   {
      "Name":"Marcelo Rocha",
      "Product_Line__c":"Business Objects",
      "Role":"Regional Manager, SAP Support Delivery, LATAM"
   },
   {
      "Name":"Marcelo Rocha",
      "Product_Line__c":"Oracle Technology",
      "Role":"Regional Manager, SAP Support Delivery, LATAM"
   },
   {
      "Name":"Leonardo Gracioli",
      "Product_Line__c":"SAP",
      "Role":"Senior Support Engineer, SAP"
   },
   {
      "Name":"Leonardo Gracioli",
      "Product_Line__c":"Business Objects",
      "Role":"Senior Support Engineer, SAP"
   },
   {
      "Name":"Leonardo Gracioli",
      "Product_Line__c":"Oracle Technology",
      "Role":"Senior Support Engineer, SAP"
   },
   {
      "Name":"Adriane Yonamine",
      "Product_Line__c":"SAP",
      "Role":"SAP Consultant"
   }
]

Combine the "Name" by ProductLime in order to be like this:

[
   {
      "Name":"Marcelo Rocha",
      "Product_Line__c":"SAP,Business Objects,Oracle Technology",
      "Role":"Regional Manager, SAP Support Delivery, LATAM"
   },
   {
      "Name":"Leonardo Gracioli",
      "Product_Line__c":"SAP,Business Objects,Oracle Technology",
      "Role":"Senior Support Engineer, SAP"
   },
   {
      "Name":"Adriane Yonamine",
      "Product_Line__c":"SAP",
      "Role":"SAP Consultant"
   }
]

Please could anyone know an easier way to do it?

CodePudding user response:

function sortUsers(Role, arr) {
  let result = [];
  arr.filter(person => person.Product_Line__c === Role).forEach(person => {
     result.push({ Name: person.Name, Product_Line__c: arr.filter(obj => obj.Name === person.Name).map(obj => obj.Product_Line__c).join(','), Role: person.Role}) 
  });
  return result;
}

const arr = [
   {
      "Name":"Marcelo Rocha",
      "Product_Line__c":"SAP",
      "Role":"Regional Manager, SAP Support Delivery, LATAM"
   },
   {
      "Name":"Marcelo Rocha",
      "Product_Line__c":"Business Objects",
      "Role":"Regional Manager, SAP Support Delivery, LATAM"
   },
   {
      "Name":"Marcelo Rocha",
      "Product_Line__c":"Oracle Technology",
      "Role":"Regional Manager, SAP Support Delivery, LATAM"
   },
   {
      "Name":"Leonardo Gracioli",
      "Product_Line__c":"SAP",
      "Role":"Senior Support Engineer, SAP"
   },
   {
      "Name":"Leonardo Gracioli",
      "Product_Line__c":"Business Objects",
      "Role":"Senior Support Engineer, SAP"
   },
   {
      "Name":"Leonardo Gracioli",
      "Product_Line__c":"Oracle Technology",
      "Role":"Senior Support Engineer, SAP"
   },
   {
      "Name":"Adriane Yonamine",
      "Product_Line__c":"SAP",
      "Role":"SAP Consultant"
   }
];

console.log(sortUsers("SAP", arr));

CodePudding user response:

I like @Haim Abeles answer as it's very elegant, but it also seems to rely on the Role Property containing the word "SAP"

I have a much more verbose, less elegant code explaining what going on.

const arr = [{
    "Name": "Marcelo Rocha",
    "Product_Line__c": "SAP",
    "Role": "Regional Manager, SAP Support Delivery, LATAM"
  },
  {
    "Name": "Marcelo Rocha",
    "Product_Line__c": "Business Objects",
    "Role": "Regional Manager, SAP Support Delivery, LATAM"
  },
  {
    "Name": "Marcelo Rocha",
    "Product_Line__c": "Oracle Technology",
    "Role": "Regional Manager, SAP Support Delivery, LATAM"
  },
  {
    "Name": "Leonardo Gracioli",
    "Product_Line__c": "SAP",
    "Role": "Senior Support Engineer, SAP"
  },
  {
    "Name": "Leonardo Gracioli",
    "Product_Line__c": "Business Objects",
    "Role": "Senior Support Engineer, SAP"
  },
  {
    "Name": "Leonardo Gracioli",
    "Product_Line__c": "Oracle Technology",
    "Role": "Senior Support Engineer, SAP"
  },
  {
    "Name": "Adriane Yonamine",
    "Product_Line__c": "SAP",
    "Role": "SAP Consultant"
  }
];

// new home for results
let combinedResults = [];

// Obtain an arrayOfUniqueNames
var arrayOfUniqueNames = [...new Set(arr.map(x => x.Name))];

// Iterate through the list
arrayOfUniqueNames.forEach(name => {

  // filter through the source array 
  // to get an arrayOfObjectsForThisName
  let arrayOfObjectsForThisName = arr.filter((obj) => obj.Name === name);

  // Create a productLinesCSVForThisName
  let productLinesCSVForThisName = arrayOfObjectsForThisName.map(el => el.Product_Line__c).join();

  // Clone the first object in the source array and replace
  // property Product_Line__c with productLinesCSVForThisName
  let aSourceObjToClone = arr.find(o => o.Name === name);
  let aClonedSOurceObject = Object.assign({}, aSourceObjToClone);
  aClonedSOurceObject.Product_Line__c = productLinesCSVForThisName;

  // Push the newly created object into the combinedResults array
  combinedResults.push(aClonedSOurceObject);
});

console.log(combinedResults);

  • Related