0

Does anyone know of to group an array from the given object then create a new array of objects? For example, I have an array of student objects:

Keep the array with its object and adding subject into an array please help me out find the below example.

var metaData = [
      {
          "student_name":'john'
          "student_class":'ten'
          "age":'16'
          "subject":'maths'
      },
      {
        "student_name":'john'
        "student_class":'ten'
        "age":'16'
        "subject":'science'
    },
    {
        "student_name":'john'
        "student_class":'ten'
        "age":'16'
        "subject":'arts'
    },{
        "student_name":'john'
        "student_class":'ten'
        "age":'16'
        "subject":'soical'
    },
    {
        "student_name":'john'
        "student_class":'ten'
        "age":'16'
        "subject":'biology'
    },
    
    {
        "student_name":'bellic'
        "student_class":'ninth'
        "age":'15'
        "subject":'general knowleadge'
    },
    {
      "student_name":'bellic'
      "student_class":'ninth'
      "age":'15'
      "subject":'games'
  },
  {
      "student_name":'bellic'
      "student_class":'ninth'
      "age":'15'
      "subject":'computer'
  },{
      "student_name":'bellic'
      "student_class":'ninth'
      "age":'15'
      "subject":'chemistry'
  },
  {
      "student_name":'bellic'
      "student_class":'ninth'
      "age":'15'
      "subject":'biology'
  },
  ]

I want to make a new array of subjects array inside new array of objects`

 var output:[
    {
        "student_name":'john'
        "student_class":'ten'
        "age":'16'
        "subject":['maths','science','arts','soical','biology']
    },
      {
        "student_name":'bellic'
        "student_class":'ninth'
        "age":'15'
        "subject":['general knowleadge','games','computer','chemistry','biology']
    }
  ]

I want to make it work with the function above.

1

3 Answers 3

2

Using Object.values and Array.prototype.reduce this is fairly straightforward:

const result = Object.values(metaData.reduce( (acc,i) => {
    const key = i.student_name + i.student_class + i.age;
    if(!acc[key])
       acc[key] = {...i, subject: [i.subject] }
    else
       acc[key].subject.push(i.subject);
    return acc;
 },{}));

Live example:

var metaData = [
      {
          "student_name":'john',
          "student_class":'ten',
          "age":'16',
          "subject":'maths'
      },
      {
        "student_name":'john',
        "student_class":'ten',
        "age":'16',
        "subject":'science'
    },
    {
        "student_name":'john',
        "student_class":'ten',
        "age":'16',
        "subject":'arts'
    },{
        "student_name":'john',
        "student_class":'ten',
        "age":'16',
        "subject":'soical'
    },
    {
        "student_name":'john',
        "student_class":'ten',
        "age":'16',
        "subject":'biology'
    },
    
    {
        "student_name":'bellic',
        "student_class":'ninth',
        "age":'15',
        "subject":'general knowleadge'
    },
    {
      "student_name":'bellic',
      "student_class":'ninth',
      "age":'15',
      "subject":'games'
  },
  {
      "student_name":'bellic',
      "student_class":'ninth',
      "age":'15',
      "subject":'computer'
  },{
      "student_name":'bellic',
      "student_class":'ninth',
      "age":'15',
      "subject":'chemistry'
  },
  {
      "student_name":'bellic',
      "student_class":'ninth',
      "age":'15',
      "subject":'biology'
  },
  ]
  
 const result = Object.values(metaData.reduce( (acc,i) => {
    const key = i.student_name + i.student_class + i.age;
    if(!acc[key])
       acc[key] = {...i, subject: [i.subject] }
    else
       acc[key].subject.push(i.subject);
    return acc;
 },{}));
 
 console.log(result);

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

Comments

0

FinalResult variable hold final value!!

     const finalResult = metaData.reduce((acc, item) =>{
     const isExist = acc.some(val => item.student_name  === val.student_name && item.student_class === val.student_class && item.age === val.age);

        if(isExist){
            return acc.map(arr => {
                if(item.student_name  === arr.student_name && item.student_class === arr.student_class && item.age === arr.age){
                    arr.subject = Array.isArray(arr.subject) ? [...arr.subject, item.subject] : [item.subject]
                }
                return arr;
            })
        }else{
          acc.push(item);
          return acc;
        }

    }, [])

    console.log(finalResult);

Comments

0

There a several possible solutions. This one reduces the original Array, using Array.reduce/filter/map.

const data = getData();
const groupedData = data
  .reduce((acc, record) =>
    !acc.find(v => v.student_name === record.student_name) ? [...acc,
      { ...record,
        subject: data
          .filter(v => v.student_name === record.student_name)
          .map(s => s.subject)
      }
    ] : acc, []);

console.log(groupedData);

function getData() {
  return [{
      "student_name": 'john',
      "student_class": 'ten',
      "age": '16',
      "subject": 'maths'
    },
    {
      "student_name": 'john',
      "student_class": 'ten',
      "age": '16',
      "subject": 'science',
    },
    {
      "student_name": 'john',
      "student_class": 'ten',
      "age": '16',
      "subject": 'arts'
    }, {
      "student_name": 'john',
      "student_class": 'ten',
      "age": '16',
      "subject": 'soical'
    }, {
      "student_name": 'john',
      "student_class": 'ten',
      "age": '16',
      "subject": 'biology'
    }, {
      "student_name": 'bellic',
      "student_class": 'ninth',
      "age": '15',
      "subject": 'general knowleadge'
    }, {
      "student_name": 'bellic',
      "student_class": 'ninth',
      "age": '15',
      "subject": 'games',
    }, {
      "student_name": 'bellic',
      "student_class": 'ninth',
      "age": '15',
      "subject": 'computer'
    }, {
      "student_name": 'bellic',
      "student_class": 'ninth',
      "age": '15',
      "subject": 'chemistry'
    }, {
      "student_name": 'bellic',
      "student_class": 'ninth',
      "age": '15',
      "subject": 'biology'
    },
  ];
}

Comments

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.