Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Associative array from a flat array based on dotted key parent value

I have an issue in creating a formatted array from a flat array with parent values which are in dot separated format. Like ..

  • Admin is a parent of user : user parent admin
  • user is a parent of test : Test parent is admin.user.

The format I want the data in is.

admin: {
    data: {...admin obj..},
    children: [{
        user: {
                data: {...user obj...},
                children: [..and so on]
            }
    }]
}

Provided data format is

[{
    "name": "admin",
    "display_name": "Menu",
    "type": 1,
    "applies_to": null,
    "perm_parent_id": null,
    "description": "Can see the admin menu item"
}, {
    "name": "admin.users",
    "display_name": "Menu",
    "type": 1,
    "applies_to": null,
    "perm_parent_id": "admin",
    "description": null
}, {
    "name": "admin.users.edit",
    "display_name": "Edit",
    "type": 1,
    "applies_to": null,
    "perm_parent_id": "admin.users",
    "description": null
}, {
    "name": "admin.users.view",
    "display_name": "View",
    "type": 1,
    "applies_to": null,
    "perm_parent_id": "admin.users",
    "description": null
}, {
    "name": "admin.groups",
    "display_name": "Groups & Permissions",
    "type": 1,
    "applies_to": null,
    "perm_parent_id": "admin",
    "description": null
}]

What I did so far is :

 var groups = myjson.reduce(function(arr, a) {
var key = a['perm_parent_id'];
var level = keys.length;
if (a['perm_parent_id'] === null) {
arr[key] = arr[key] || {}
arr[key]["data"] = a || {};
arr[key]["children"] = [];
} else {
arr[key] = arr[key] || {}
arr[key] = arr[key] || {};
arr[key]["children"] = arr[key]["children"] || [];
arr[key]["children"].push(a);
}
return arr;
}, {})
like image 604
Fida Avatar asked May 14 '26 16:05

Fida


1 Answers

You could use a nested hash table approach by splitting the name and walk the given information by taking for each step an own hash key with the given information.

var data = [{ name: "admin", display_name: "Menu", type: 1, applies_to: null, perm_parent_id: null, description: "Can see the admin menu item" }, { name: "admin.users", display_name: "Menu", type: 1, applies_to: null, perm_parent_id: "admin", description: null }, { name: "admin.users.edit", display_name: "Edit", type: 1, applies_to: null, perm_parent_id: "admin.users", description: null }, { name: "admin.users.view", display_name: "View", type: 1, applies_to: null, perm_parent_id: "admin.users", description: null }, { name: "admin.groups", display_name: "Groups & Permissions", type: 1, applies_to: null, perm_parent_id: "admin", description: null }],
    result = [],
    hash = { _: result };

data.forEach(function (o) {
    o.name.split('.').reduce(function (r, k) {
        if (!r[k]) {
            r[k] = { _: [] };
            r._.push({ data: o, children: r[k]._ });
        }
        return r[k];
    }, hash);
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
like image 61
Nina Scholz Avatar answered May 16 '26 05:05

Nina Scholz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!