Im trying to get the full path of an array of objects by using recursion to loop over the array objects and get their path with relation to the parent, what i couldn't do was the part when the path reset and starts to add the "child" key .
const data = {
data: [
{
key: 'userInfo',
group: [
{
key: 'street',
},
{
key: 'phone',
},
],
},
{
key: 'fullName',
group: [
{
key: 'firstName',
},
{
key: 'lastName',
},
],
},
],
};
// userInfo.street
// userInfo.phone
// fullName.firstName
// fullName.lastName
let groupPath = '';
data.data.forEach(function iter(a) {
if (a.group) {
groupPath = a.key;
} else {
groupPath = [groupPath, a.key].join('.');
}
Array.isArray(a.group) && a.group.forEach(iter);
console.log(' group', groupPath);
});
The finale result should look like this :
userInfo.street
userInfo.phone
fullName.firstName
fullName.lastName