0

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
}
forgottofly
  • 2,729
  • 11
  • 51
  • 93
  • 1
    there is no array here, and no object `itemtype`. There is however a single object that has a property `itemtype` – charlietfl Dec 22 '14 at 06:51
  • possible duplicate of [Remove property from JavaScript object](http://stackoverflow.com/questions/208105/remove-property-from-javascript-object) – Quip Yowert Dec 26 '14 at 04:32

2 Answers2

2

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;
Oleksandr T.
  • 76,493
  • 17
  • 173
  • 144
0

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.