I am in need of converting an array of objects into a nested property template object. The nested object will be constructed based on the checked items from the array.
Initially, the first level objects have their 'checked' key as null, implying that they will be included at the beginning of the property template object.
If the first level objects do not have any properties or none of the properties are marked as checked, the output should resemble this:
{staff: true}
However, if the objects contain properties and some of them are checked, the output structure will look like this:
{staffedLocation: ['schedulingGroup',
{property: 'staff', subProperties: ['description']}
]
}
This is just a basic example, and sometimes the array of nested objects can go even deeper, necessitating the use of a recursive function. I'm struggling with how to implement recursion when the structure of the first level object differs from the rest.
An example of the array of objects being worked with is shown below:
[
{checked: null, name: "staffedLocation", properties: [
{checked: null, name: "oid", properties: null, toggle: null, type: "integer"},
{checked: null, name: "_class", properties: null, toggle: null, type: "string"},
{checked: true, name: "schedulingGroups", properties: null, toggle: null, type: "list"},
{checked: null, name: "staff", properties: [
{checked: null, name: "oid", properties: null, toggle: null, type: "integer"},
{checked: null, name: "_class", properties: null, toggle: null, type: "string"},
{checked: true, name: "description", properties: null, toggle: null, type: "string"},
{checked: null, name: "limits", properties: null, toggle: null, type: "list"},
{checked: null, name: "weeklyMaxHours", properties: null, toggle: null, type: "integer"}
], toggle: true, type: "list"},
], toggle: true, type: "staffedLocation"},
{checked: null,
name: "staff", properties: [
{checked: null, name: "oid", properties: null, toggle: null, type: "integer"},
{checked: null, name: "_class", properties: null, toggle: null, type: "string"},
{checked: null, name: "description", properties: null, toggle: null, type: "string"},
{checked: null, name: "limits", properties: null, toggle: null, type: "list"},
{checked: null, name: "weeklyMaxHours", properties: null, toggle: null, type: "integer"}
], toggle: true, type: "staff"},
{checked: null, name: "assignedShifts", properties: null, toggle: null, type: "shiftForEmail"},
{checked: null, name: "editedShifts", properties: null, toggle: null, type: "shiftForEmail"},
{checked: null, name: "deletedShifts", properties: null, toggle: null, type: "shiftForEmail"},
{checked: null, name: "unassignedShifts", properties: null, toggle: null, type: "shiftForEmail"},
{checked: null, name: "rangeStart", properties: null, toggle: null, type: "timestamp"},
{checked: null, name: "rangeEnd", properties: null, toggle: null, type: "timestamp"}
]
The desired output should be a nested object structured like this:
{
staffedLocation: ['schedulingGroup',{property: 'staff',subProperties: ['description']}],
staff: true,
assignedShifts: true,
editedShifts: true,
deletedShifts: true,
unassignedShifts: true,
rangeStart: true,
rangeEnd: true
}