从数组中删除对象

时间:2014-12-22 06:49:28

标签: javascript arrays arrayobject

从下面给定的对象中删除名为" itemType"的属性的方法是什么? ?

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

这样最终的数组应该看起来像

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

2 个答案:

答案 0 :(得分:2)

这是object而非array。您可以像这样使用delete

   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;

答案 1 :(得分:0)

无论是对象还是想从中删除属性,或者你有一个数组并想要删除一个值,删除的方法是 -

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

注意 - 您提供的结构不是数组,而是一个对象。