Create one new array from given array and get different properties as a Child object/Array

I have Responses array with multiple id but different question answers.

Responses = 
[0:{Id : 1,Name : John, QuestionId :1,Answer :8},
1:{Id : 1,Name : John, QuestionId :2,Answer :9},
2:{Id : 1,Name : John, QuestionId :3,Answer :3},
3:{Id : 1,Name : John, QuestionId :4,Answer :2},
4:{Id : 2,Name : Mark, QuestionId :1,Answer :6},
5:{Id : 2,Name : Mark, QuestionId :2,Answer :2},
6:{Id : 2,Name : Mark, QuestionId :3,Answer :5},
7:{Id : 2,Name : Mark, QuestionId :4,Answer :1}]
----------------------------------------------------------------
Output I want :
 
Arr = 
[0:{Id : 1,Name : John,
 Question : [{QuestionId :1,Answer :9},{QuestionId :2,Answer :8},{QuestionId :3,Answer :3},{QuestionId :4,Answer :2}]},
1:{Id : 2,Name : Mark,
 Question : [{QuestionId :1,Answer :6},{QuestionId :2,Answer :2},{QuestionId :3,Answer :5},{QuestionId :4,Answer :1}]}]
---------------------------------------------------------

I want to create one array with Id, Name and one Question object which would have Question Id with answer. I tried different way but that didn’t work for me. It would be great if you can help me here. Thank you.

6 thoughts on “Create one new array from given array and get different properties as a Child object/Array”

  1.     const a = [{Id : 1,Name : 'John', QuestionId :1,Answer :8},
    {Id : 1,Name : 'John', QuestionId :2,Answer :9},
    {Id : 1,Name : 'John', QuestionId :3,Answer :3},
    {Id : 1,Name : 'John', QuestionId :4,Answer :2},
    {Id : 2,Name : 'Mark', QuestionId :1,Answer :6},
    {Id : 2,Name : 'Mark', QuestionId :2,Answer :2},
    {Id : 2,Name : 'Mark', QuestionId :3,Answer :5},
    {Id : 2,Name : 'Mark', QuestionId :4,Answer :1}]
    
    
    const result = a.reduce((acc,curVal,index,src)=>{
        const {Id, Name, QuestionId,Answer} = curVal
        if(index===0 ){
            acc.push({Id,Name,Question:[{QuestionId, Answer}]})
        }else{
        const itemId = acc.findIndex(el=>el.Id===curVal.Id)
      if(itemId>= 0){
        acc[itemId].Question.push({QuestionId, Answer})
      }else{
        acc.push({Id,Name,Question:[{QuestionId, Answer}]})
      }
    }
    
    return acc
    },[])
    
    console.log(result)
    
    Reply
  2. You will first have to group your data by Name. After you have your grouped data, map the values. When mapping, you can destructure the first question’s Id and Name fields and then map the QuestionId and Answer fields to the Question field.

    const groupBy = (arr, key) => arr.reduce((acc, item) => ({
      ...acc,
      [item[key]]: [ ...(acc[[item[key]]] ?? []), item ]
    }), {});
    
    const responses = [
      { "Id": 1, "Name": "John", "QuestionId": 1, "Answer": 8 },
      { "Id": 1, "Name": "John", "QuestionId": 2, "Answer": 9 },
      { "Id": 1, "Name": "John", "QuestionId": 3, "Answer": 3 },
      { "Id": 1, "Name": "John", "QuestionId": 4, "Answer": 2 },
      { "Id": 2, "Name": "Mark", "QuestionId": 1, "Answer": 6 },
      { "Id": 2, "Name": "Mark", "QuestionId": 2, "Answer": 2 },
      { "Id": 2, "Name": "Mark", "QuestionId": 3, "Answer": 5 },
      { "Id": 2, "Name": "Mark", "QuestionId": 4, "Answer": 1 }
    ];
    
    const arr = Object.values(groupBy(responses, 'Name')).map(questions => {
      const [ { Id, Name } ] = questions;
      return {
        Id, Name, Question: questions.map(({ QuestionId, Answer }) =>
          ({ QuestionId, Answer }))
      };
    });
    
    console.log(arr);
    .as-console-wrapper { top: 0; max-height: 100% !important; }
    Reply
  3. const arr = [ 
      {Id : 1,Name : "John", QuestionId :1,Answer :8},
      {Id : 1,Name : "John", QuestionId :2,Answer :9},
      {Id : 1,Name : "John", QuestionId :3,Answer :3},
      {Id : 1,Name : "John", QuestionId :4,Answer :2},
      {Id : 2,Name : "Mark", QuestionId :1,Answer :6},
      {Id : 2,Name : "Mark", QuestionId :2,Answer :2},
      {Id : 2,Name : "Mark", QuestionId :3,Answer :5},
      {Id : 2,Name : "Mark", QuestionId :4,Answer :1}
    ]
    
    let newObj = {};
    arr.forEach(({ Id, Name, QuestionId, Answer }) => {
      if(newObj[Id]) {
        newObj[Id]["Question"] = [
          ...newObj[Id]["Question"],
          { QuestionId, Answer }
        ]
      } else {
        newObj[Id] = {
          Id,
          Name,
          Question: [{ QuestionId, Answer }]
        } 
      }
    })
    
    let newArr = [];
    for(let key in newObj) {
      newArr.push(newObj[key]);
    }
    
    console.log(newArr); 
    
    Reply

Leave a Comment