Home > Back-end >  Update array of nested object value
Update array of nested object value

Time:12-02

I want to update the array of nested object values below I have given my object. the first object of name things objects of permission object View value I want to update as false.

const obj=  [
    {
        "name": "Things",
        "is_default": true,
        "item_id": "60aa485d-551e-4d96-8355-68c3787f17d7",
        "permissions": {
            "View": true,
            "Create": false,
            "Delete": false,
            "Update": false
        },
        "description": "Things component permissions"
    },
    {
        "name": "Devices",
        "is_default": true,
        "item_id": "eb3efccd-8bab-4065-b350-693bcf2024a6",
        "permissions": {
            "View": true,
            "Create": false,
            "Delete": false,
            "Update": false
        },
        "description": "Devices component permissions"
    }
]

I want to update the first object of permission object View is false

expected output is:

const obj=  [
    {
        "name": "Things",
        "is_default": true,
        "item_id": "60aa485d-551e-4d96-8355-68c3787f17d7",
        "permissions": {
            "View": false,
            "Create": false,
            "Delete": false,
            "Update": false
        },
        "description": "Things component permissions"
    },
    {
        "name": "Devices",
        "is_default": true,
        "item_id": "eb3efccd-8bab-4065-b350-693bcf2024a6",
        "permissions": {
            "View": true,
            "Create": false,
            "Delete": false,
            "Update": false
        },
        "description": "Devices component permissions"
    }
]

Thanks in advance please help anyone...

CodePudding user response:

I'd suggest creating a function, say setPermission to get the relevant entry, then set the permissions appropriately:

const obj=  [ { "name": "Things", "is_default": true, "item_id": "60aa485d-551e-4d96-8355-68c3787f17d7", "permissions": { "View": true, "Create": false, "Delete": false, "Update": false }, "description": "Things component permissions" }, { "name": "Devices", "is_default": true, "item_id": "eb3efccd-8bab-4065-b350-693bcf2024a6", "permissions": { "View": true, "Create": false, "Delete": false, "Update": false }, "description": "Devices component permissions" } ]

function setPermission(arr, index, permissionName, value) {
    let entry = arr[index];
    if (entry) {
       entry.permissions[permissionName] = value;
    }
}

setPermission(obj, 0, 'View', false);

console.log(obj)
 
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

You could also search for items by id, for example:

const obj=  [ { "name": "Things", "is_default": true, "item_id": "60aa485d-551e-4d96-8355-68c3787f17d7", "permissions": { "View": true, "Create": false, "Delete": false, "Update": false }, "description": "Things component permissions" }, { "name": "Devices", "is_default": true, "item_id": "eb3efccd-8bab-4065-b350-693bcf2024a6", "permissions": { "View": true, "Create": false, "Delete": false, "Update": false }, "description": "Devices component permissions" } ]

function setPermissionById(arr, id, permissionName, value) {
    let entry = arr.find(el => el.item_id === id);
    if (entry) {
       entry.permissions[permissionName] = value;
    }
}

setPermissionById(obj, '60aa485d-551e-4d96-8355-68c3787f17d7', 'View', false);

console.log(obj)
 
.as-console-wrapper { max-height: 100% !important; top: 0; }
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related