0

I have getting some data which is somewhat look like this.

var myData = [{Name : "Alex",Roll : 1,Class :   1,Marks :[{Maths: 100,Science : 200}],Weight: 50},        
            {Name : "Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 40},
            {Name : "Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 30},
            {Name : "Alex",Roll : 1,Class :1,   Marks :[{Maths: 100,Science : 200, }],Weight: 60},
            {Name : "Cean",Roll : 3,Class :1,   Marks :[{Physic: 100,Economics : 200, }],Weight: 40}]

What I have tried is this code.

function groupBy(xs, f) {
  return xs.reduce((r, v, i, a, k = f(v)) => ((r[k] || (r[k] = [])).push(v), r), {});
}

This is grouping the data but I want to store into array. Two keys should be outside and after that remaining stuff should be inside an array under collection.

newData = [{Name : "Alex",
            Roll: 1, 
            collection :[{Name : "Alex",Roll : 1,Class :    1,Marks :[{Maths: 100,Science : 200}],Weight: 50},
                         {Name : "Alex",Roll : 1,Class :1,  Marks :[{Maths: 100,Science : 200, }],Weight: 60}]
            },
            {Name : "Brat",
            Roll: 2, 
            collection :[{Name : "Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 40},
                         {"Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 30}]
            },
            {Name : "Cean",
            Roll: 3, 
            collection :[{Name : "Cean",Roll : 3,Class :1,  Marks :[{Physic: 100,Economics : 200, }],Weight: 40}],
            }
            ]

Can anybody help me to modify my code and help meto reach the solution.

Objext

David
  • 4,266
  • 8
  • 34
  • 69
  • @Rajesh No this is not duplicate. I have to store my data into array, There data is adding as object. – David Sep 20 '19 at 12:31
  • Group the values and then use `Object.values` to get array of values or loop over keys to get value array: `Object.keys(obj).map((key) => obj[key])` should do it – Rajesh Sep 20 '19 at 12:34
  • @Rajesh But in this case grouping is not happening right. – David Sep 23 '19 at 05:58

1 Answers1

2

You can use Roll as key, if it's present add the current element to collection property of that particular Roll else initialize with default structure

const myData = [{Name : "Alex",Roll : 1,Class : 1,Marks :[{Maths: 100,Science : 200}],Weight: 50},{Name : "Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 40},{Name : "Brat",Roll : 2,Class : 2,Marks :[{English: 100,History : 200 }],Weight: 30},{Name : "Alex",Roll : 1,Class :1,   Marks :[{Maths: 100,Science : 200, }],Weight: 60},{Name : "Cean",Roll : 3,Class :1,   Marks :[{Physic: 100,Economics : 200, }],Weight: 40}]            
 
let grouped = myData.reduce( (op,inp) => {
  let { Roll } = inp
  if( op[Roll] ) {
    op[Roll].collection.push(inp)
  } else{
    op[Roll] = {Name: inp.Name, Roll, collection:[{...inp}]}
  }
  return op
},{})

console.log(Object.values(grouped))
Code Maniac
  • 37,143
  • 5
  • 39
  • 60
  • Yes, I can make Roll as a key and check the condition. Thanks Great – David Sep 20 '19 at 12:39
  • But this is not adding into array. This is behaving like object inside object. I have attach the image in question. @CodeManiac – David Sep 23 '19 at 04:54
  • @David this is giving the expected result, as you mentioned in question, did you tried running the snippet of answer ? most probably you're not using `Object.values(grouped)` to get values in the form of array – Code Maniac Sep 23 '19 at 05:16
  • Yes I missed that part. Thanks :) – David Sep 23 '19 at 06:06