Remove an object from an array

56 Views Asked by At

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
}
2

There are 2 best solutions below

0
On BEST ANSWER

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;
4
On

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.