如何从JavaScript对象中删除元素

时间:2013-10-18 08:10:43

标签: javascript jquery javascript-objects

我有对象名Roption,我想删除Add Task中的第三个元素。

这一个=> "{ text: "Child", alias: "addsubbelowchild", action:menuAction}"

我正在使用删除Roption.items [0] .items [2]

它可以正常工作,但Roption.items [0] .items.length即使在删除后也会返回相同的计数。

是真的删除还是只是未定义?

 var Roption = { width: 150, items: [
                { text: "Add Task",  alias: "addtask", type: "group" , width : 130 , items : 
                    [   
                        { text: "Sibling",  alias: "addsiblingbelow", action : menuAction},
                        { text: "Child First", alias: "addsubaboveDirect", action: menuAction },
                        { text: "Child Last",  alias: "addsubbelowDirect", action: menuAction },
                        { text: "Child",  alias: "addsubbelowchild", action:menuAction},
                        { text: "Advanced Add",  alias: "advancedadd", type: "group" , width : 120 , items :
                            [
                                { text: "Sibling", alias: "addsibling", type: "group" , width : 120 ,items :
                                    [
                                        { text: "Below",  alias: "addsiblingbelow", action: menuAction },
                                        { text: "Above",  alias: "addsiblingabove", action: menuAction }
                                    ]
                                },
                                { text: "Child", alias: "addsubtask", type: "group" , width : 120 ,items :
                                    [
                                        { text: "First", alias: "addsubabove", action: menuAction },
                                        { text: "Last", alias: "addsubbelow", action: menuAction }
                                    ]
                                }
                            ]
                        }
                    ]
                }
            ]
                , onShow: applyrule
                , onContextMenu: BeforeContextMenu
                , onClose : AfterContextMenu
};
function menuAction(row) {}
function BeforeContextMenu(row) {}
function applyrule(row) {}
function AfterContextMenu(row) {}

1 个答案:

答案 0 :(得分:1)

尝试使用.splice(index,howmany)

Roption.items[0].items.splice(2,1);