1

I am working on a small React project where I am stuck at this point. I need to make a grouping based on array object key.

Below is the Array:

   let addOnFeatures = [
    {"name": "feature1", id: 101, cost: 100},
    {"name": "feature2", id: 102, cost: 200},
    {"name": "feature1", id: 103, cost: 300},
    {"name": "feature3", id: 104, cost: 40}
    ]

I need to make a grouping based on name key so that every id related to common name should be displayed on its own tab.

Expected output should be like

*uniqueFeaturename
id: cost

Example:

 1. feature1
   101 : 100
   103 : 300
 2. feature2
    102 : 200
 3. feature3
    104: 40
3
  • Please post the expected output in a valid JSON format with the code you've tried. It's unclear at the moment Commented May 28, 2020 at 6:35
  • Hi! I just edited your question, so that the text is out of the code-formatted blocks. Commented May 28, 2020 at 6:38
  • Second comment: what have you tried so far? Please expose either some code, or at least the algorithm you'd like to implement. Commented May 28, 2020 at 6:39

2 Answers 2

2

Just make use of Array.prototype.reduce and add elements to object like

let addOnFeatures = [
    {"name": "feature1", id: 101, cost: 100},
    {"name": "feature2", id: 102, cost: 200},
    {"name": "feature1", id: 103, cost: 300},
    {"name": "feature3", id: 104, cost: 40}
]

const res = addOnFeatures.reduce((acc, item) => {
   if(acc[item.name]) {
      acc[item.name] = {...acc[item.name], [item.id]: item.cost};
   } else {
      acc[item.name] = {[item.id]: item.cost};
   }
   return acc;
}, {});

console.log(res);

Sign up to request clarification or add additional context in comments.

Comments

-1

One solution can be this:

 let addOnFeatures = [
    {"name": "feature1", id: 101, cost: 100},
    {"name": "feature2", id: 102, cost: 200},
    {"name": "feature1", id: 103, cost: 300},
    {"name": "feature3", id: 104, cost: 40}
    ];
    
    var res={}; 
    addOnFeatures.forEach(i=>{
    
       if(!res[i.name]){
         res[i.name] = [{id: i.id, cost:i.cost}]
       }else{
        res[i.name].push({id: i.id, cost:i.cost})
       }
    }
    );
    
    console.log(res)
Or this

 let addOnFeatures = [
    {"name": "feature1", id: 101, cost: 100},
    {"name": "feature2", id: 102, cost: 200},
    {"name": "feature1", id: 103, cost: 300},
    {"name": "feature3", id: 104, cost: 40}
    ];
    
   var res2={}; 
    addOnFeatures.forEach(i=>{
        res2[i.name] = res2[i.name] ? [...res2[i.name],...[{id: i.id, cost:i.cost}]] :[...[{id: i.id, cost:i.cost}]]
    }
    );
    
    console.log(res2)

1 Comment

This one should work, but use a reducer instead of mutate a var with a forEach, is quitely better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.