Ad

Array Data Structure In Nodejs

I'm working on a nodejs project where I need to restructure an array of objects. Below is the sample out put I get from database, that need to be restructured.

"data": [
    {
      "ObtainedMarks": null,
      "ObtainNumText": "1",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Session",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-01",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Start date ",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-01",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "End date ",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "ahmedabad",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Overall Feeling",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "high light",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Highlights",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "low light",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Lowlights",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 1,
      "ObtainNumText": null,
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Program rating(0-10)",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 1,
      "ObtainNumText": null,
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Venue rating(0-10)",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "photo",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Program photos",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "video",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Program videos",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 10,
      "ObtainNumText": null,
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "No. of Participants present",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 8,
      "ObtainNumText": null,
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "No. of Participants fees paid",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 80,
      "ObtainNumText": null,
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Total fees received",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-01",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Next program date",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "program remark",
      "RefID": 312,
      "EmpID": 1613,
      "ChapterNameEnglish": "Remarks",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "1",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Session",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-01",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Start date ",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-08",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "End date ",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "ap",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Overall Feeling",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "ap",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Highlights",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "ap",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Lowlights",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 10,
      "ObtainNumText": null,
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Program rating(0-10)",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 10,
      "ObtainNumText": null,
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Venue rating(0-10)",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "ap",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Program photos",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "ap",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Program videos",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 10,
      "ObtainNumText": null,
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "No. of Participants present",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 10,
      "ObtainNumText": null,
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "No. of Participants fees paid",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": 10,
      "ObtainNumText": null,
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Total fees received",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-08",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Next program date",
      "EmpName": "Oasis Center User"
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "testing",
      "RefID": 1315,
      "EmpID": 1613,
      "ChapterNameEnglish": "Remarks",
      "EmpName": "Oasis Center User"
    }
  ]
}

Above is the result I'm getting from database. The data need to be structured this way....

0: {
    "EmpName": "Oasis Center User"
     "RefID": 312,
   {
      "ObtainedMarks": null,
      "ObtainNumText": "1",
      "EmpID": 1613,
      "ChapterNameEnglish": "Session",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-01",
      "EmpID": 1613,
      "ChapterNameEnglish": "Start date ",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-01",
      "EmpID": 1613,
      "ChapterNameEnglish": "End date ",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "ahmedabad",
      "EmpID": 1613,
      "ChapterNameEnglish": "Overall Feeling",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "high light",
      "EmpID": 1613,
      "ChapterNameEnglish": "Highlights",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "low light",
      "EmpID": 1613,
      "ChapterNameEnglish": "Lowlights",
    },
    {
      "ObtainedMarks": 1,
      "ObtainNumText": null,     "EmpID": 1613,
      "ChapterNameEnglish": "Program rating(0-10)",
    },
    {
      "ObtainedMarks": 1,
      "ObtainNumText": null,
      "EmpID": 1613,
      "ChapterNameEnglish": "Venue rating(0-10)",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "photo",
      "EmpID": 1613,
      "ChapterNameEnglish": "Program photos",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "video",
      "EmpID": 1613,
      "ChapterNameEnglish": "Program videos",    },
    {
      "ObtainedMarks": 10,
      "ObtainNumText": null,
      "EmpID": 1613,
      "ChapterNameEnglish": "No. of Participants present",
    },
    {
      "ObtainedMarks": 8,
      "ObtainNumText": null,
      "EmpID": 1613,
      "ChapterNameEnglish": "No. of Participants fees paid",
    },
    {
      "ObtainedMarks": 80,
      "ObtainNumText": null,
      "EmpID": 1613,
      "ChapterNameEnglish": "Total fees received",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "2020-01-01",
      "EmpID": 1613,
      "ChapterNameEnglish": "Next program date",
    },
    {
      "ObtainedMarks": null,
      "ObtainNumText": "program remark",
      "EmpID": 1613,
      "ChapterNameEnglish": "Remarks",
    },

},
.
.
.
.

I'm quite a new to the nodejs environment, How to achive this in nodejs.... ?

Ad

Answer

I'm still not sure how you want to group it data here. Maybe you can do something like this:

data=[ { "ObtainedMarks": null, "ObtainNumText": "1", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Session", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "2020-01-01", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Start date ", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "2020-01-01", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "End date ", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "ahmedabad", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Overall Feeling", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "high light", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Highlights", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "low light", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Lowlights", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 1, "ObtainNumText": null, "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Program rating(0-10)", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 1, "ObtainNumText": null, "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Venue rating(0-10)", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "photo", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Program photos", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "video", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Program videos", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 10, "ObtainNumText": null, "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "No. of Participants present", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 8, "ObtainNumText": null, "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "No. of Participants fees paid", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 80, "ObtainNumText": null, "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Total fees received", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "2020-01-01", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Next program date", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "program remark", "RefID": 312, "EmpID": 1613, "ChapterNameEnglish": "Remarks", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "1", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Session", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "2020-01-01", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Start date ", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "2020-01-08", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "End date ", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "ap", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Overall Feeling", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "ap", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Highlights", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "ap", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Lowlights", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 10, "ObtainNumText": null, "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Program rating(0-10)", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 10, "ObtainNumText": null, "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Venue rating(0-10)", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "ap", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Program photos", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "ap", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Program videos", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 10, "ObtainNumText": null, "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "No. of Participants present", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 10, "ObtainNumText": null, "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "No. of Participants fees paid", "EmpName": "Oasis Center User" }, { "ObtainedMarks": 10, "ObtainNumText": null, "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Total fees received", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "2020-01-08", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Next program date", "EmpName": "Oasis Center User" }, { "ObtainedMarks": null, "ObtainNumText": "testing", "RefID": 1315, "EmpID": 1613, "ChapterNameEnglish": "Remarks", "EmpName": "Oasis Center User" } ]

var result = data.reduce((acc, {EmpID,RefID,...rest})=>{
  acc[EmpID]=acc[EmpID] || { EmpID };
  acc[EmpID][RefID] = acc[EmpID][RefID] || { EmpName:rest.EmpName,RefID,data:[] };
  acc[EmpID][RefID].data.push(rest);
  return acc;
},{});

console.log(result);

Ad
source: stackoverflow.com
Ad