简体   繁体   中英

Remove an object from an array

What is the way to remove an property named "itemType"from the below given object ?

 {
    "id": 19,
    "cost": 10,
    "items": 10,
    "numbers": 10,
    "status": false,
    "hours": 10,
    "itemType": {
        "id": 16,
        "name": "PC 350",
        "description": "PC 350"        
    },
    "typeid": 12
}

So that the final array should look like

 {
    "id": 19,
    "cost": 10,
    "items": 10,
    "numbers": 10,
    "status": false,
    "hours": 10,         
    "typeid": 12
}

This is object not array . You can use delete like this

   var obj = {
        "id": 19,
        "cost": 10,
        "items": 10,
        "numbers": 10,
        "status": false,
        "hours": 10,
        "itemType": {
            "id": 16,
            "name": "PC 350",
            "description": "PC 350"        
        },
        "typeid": 12
    }

    delete obj.itemType;

Whether it is an object and you want to delete a property from it, or you have an array and want to delete a value, the way to delete is -

delete obj.itemType //object.
delete array[3] //delete 4th item from the array.

Note - the structure you have provided is not an array, its an object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM