Home > Enterprise >  Convert an object to 3d array
Convert an object to 3d array

Time:09-25

I am new to node.js and javascript. I am trying to build a rest API.

For a specific purpose, I need to convert an object to 3d array.

I tried running for, foreach loop but it is not providing what I am trying to achieve.

My object looks like this.

"data": [
    {
        "id": 15184,
        "badge_id": "330886",
        "name": "Rukmani J. Solanki",
        "gender": "Female",
        "type": "PF",
        "department": "Sales",
        "checkin": "2021-09-24T08:52:44.000Z",
        "checkout": "2021-09-24T08:57:45.000Z",
        "hours": "00:05"
    },
    {
        "id": 15185,
        "badge_id": "440886",
        "name": "Jairam J. Solanki",
        "gender": "Male",
        "type": "PM",
        "department": "Sales",
        "checkin": "2021-09-24T09:28:32.000Z",
        "checkout": null,
        "hours": null
    }
]

And I want something like this

{
    "onfield": [
        {
            "key": "Sales",
            "data": {
                "PM": [
                    {
                        "name": "Gokuldas S. Sundrani",
                        "badge_id": "441101",
                        "gender": "Male",
                        "checkIn": "2021-09-24 06:04:18",
                        "checkOut": null,
                        "hours": "NoCheckOut"
                    },
                    {
                        "name": "Satnamsingh M. Chhabra",
                        "badge_id": "551249",
                        "gender": "Male",
                        "checkIn": "2021-09-24 06:47:31",
                        "checkOut": "2021-09-24 08:32:00",
                        "hours": "1.7 Hours"
                    },
                    {
                        "name": "Praveen N. Jethwani",
                        "badge_id": "771328",
                        "gender": "Male",
                        "checkIn": "2021-09-24 07:14:11",
                        "checkOut": "2021-09-24 08:29:34",
                        "hours": "1.3 Hours"
                    },
                    {
                        "name": "Satnamsingh M. Chhabra",
                        "badge_id": "88249",
                        "gender": "Male",
                        "checkIn": "2021-09-24 08:32:00",
                        "checkOut": null,
                        "hours": "NoCheckOut"
                    },
                    {
                        "name": "Arjundas D. Chhabra",
                        "badge_id": "661248",
                        "gender": "Male",
                        "checkIn": "2021-09-24 10:19:22",
                        "checkOut": "2021-09-24 18:38:32",
                        "hours": "8.3 Hours"
                    },
                    {
                        "name": "Parmanand C. Lalwani",
                        "badge_id": "8651418",
                        "gender": "Male",
                        "checkIn": "2021-09-24 14:51:08",
                        "checkOut": "2021-09-24 17:39:27",
                        "hours": "2.8 Hours"
                    },
                    {
                        "name": "Dhanalal G. Chouhan",
                        "badge_id": "5501392",
                        "gender": "Male",
                        "checkIn": "2021-09-24 14:58:46",
                        "checkOut": "2021-09-24 18:20:50",
                        "hours": "3.4 Hours"
                    }
                ],
                "PF": [
                    {
                        "name": "Baljeetkaur S. Chhabra",
                        "badge_id": "501993",
                        "gender": "Female",
                        "checkIn": "2021-09-24 06:47:48",
                        "checkOut": "2021-09-24 08:32:12",
                        "hours": "1.7 Hours"
                    },
                    {
                        "name": "Baljeetkaur S. Chhabra",
                        "badge_id": "801993",
                        "gender": "Female",
                        "checkIn": "2021-09-24 08:32:12",
                        "checkOut": null,
                        "hours": "NoCheckOut"
                    }
                ],
                "OM": [
                    {
                        "name": "Yadvendra Bhati",
                        "badge_id": "2255454",
                        "gender": "male",
                        "checkIn": "2021-09-24 13:38:37",
                        "checkOut": "2021-09-24 17:24:11",
                        "hours": "3.8 Hours"
                    }
                ],
                "OF": [
                    {
                        "name": "Yashoda Bhati",
                        "badge_id": "223F0029",
                        "gender": "Female",
                        "checkIn": "2021-09-24 13:38:44",
                        "checkOut": "2021-09-24 17:24:25",
                        "hours": "3.8 Hours"
                    }
                ]
            }
        }
    ]
}

How can this be done?

I will be really grateful to you for your help.

I have googled and searched StackOverflow but did not find anything which deals with this kind of problem.

Thank You

CodePudding user response:

Although I totally agree with Juan Mendes, here is a tip in order for you to be able to accomplish this. You may get an intermediate form for your data as this one:

{
  "onfield": [
    {
      "key": "dept1",
      "data": {
        "PM": [
          {
            "id": 123,
            "badge_id": "1231",
            "name": "name1",
            "gender": "Male"
          }
        ]
      }
    },
    {
      "key": "dept2",
      "data": {
        "PF": [
          {
            "id": 124,
            "badge_id": "1232",
            "name": "name2",
            "gender": "Female"
          }
        ]
      }
    },
    {
      "key": "dept1",
      "data": {
        "PM": [
          {
            "id": 125,
            "badge_id": "1233",
            "name": "name3",
            "gender": "Male",
            "type": "PM",
            "dept": "dept1"
          }
        ]
      }
    }
  ]
}

Perhaps transforming your source data to this one is easier. And getting to your target from here will be just a matter of merging arrays of the same type.

Do not hesitate to ask if this keeps being difficult. But as said, please try to solve this yourself, that's the way you are going to learn.

CodePudding user response:

let data = [{ "id": 123, "badge_id": "1231", "name": "name1", "gender": "Male", "type": "PM", "dept": "dept1" }, { "id": 124, "badge_id": "1232", "name": "name2", "gender": "Female", "type": "PF", "dept": "dept2" }, { "id": 125, "badge_id": "1233", "name": "name3", "gender": "Male", "type": "PM", "dept": "dept1" }];
let Report = []

data.forEach((item) => {
    let obj = Report.find(v => v.dept == item.dept);
    if (!obj) Report.push(obj = {
        dept: item.dept,
        type: {}
    });
    obj.type[item.type] ??= [];
    obj.type[item.type].push({
        id: item.id,
        badge_id: item.badge_id,
        name: item.name,
        gender: item.gender,
    });
})

console.log(Report)

CodePudding user response:

Here's Something i think is more feasible than your result .. Checkout

let myObject = {

  "data": [ {
    "id": 123,
    "badge_id": "1231",
    "name": "name1",
    "gender": "Male",
    "type": "PM",
    "dept": "dept1"
  },
    {
      "id": 124,
      "badge_id": "1232",
      "name": "name2",
      "gender": "Female",
      "type": "PF",
      "dept": "dept2"
    },
    {
      "id": 125,
      "badge_id": "1233",
      "name": "name3",
      "gender": "Male",
      "type": "PM",
      "dept": "dept1"
    }]
}

function generateReport(object) {
  let obj = {
    "Report": []
  };
  let  types = [];
  object.data.forEach(arr =>{
    if(!types.includes(arr.type)){
    types.push(arr.type);
    }
  });
  
  types.forEach(type =>{
    obj.Report.push({
      type : type,
      users : object.data.filter(arr=>{ let a = arr.type == type;
         if(a){
           delete arr.type;
         }
        return a;
     })
    });
  });
  return obj;
}

myObject = generateReport(myObject);
console.log(myObject)

CodePudding user response:

You oculd take the wanted grouping keys in an array and destructure the object to remove this keys from the object.

Every key takes it own nested object structure.

const
    data = [{ id: 123, badge_id: "1231", name: "name1", gender: "Male", type: "PM", dept: "dept1" }, { id: 124, badge_id: "1232", name: "name2", gender: "Female", type: "PF", dept: "dept2" }, { id: 125, badge_id: "1233", name: "name3", gender: "Male", type: "PM", dept: "dept1" }],
    keys = ['dept', 'type'],
    result = data
        .reduce((r, o) => {
            keys
                .reduce(function (t, k) {
                    let key;
                    ({ [k]: key, ...o } = o);
                    if (!t[key]) t._.push({ [k]: key, data: (t[key] = { _: [] })._ });
                    return t[key];
                }, r)
                ._
                .push(o);
            return r;
        }, { _: [] })
        ._;

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

  • Related