我如何通过对象的嵌套数组迭代和删除一些属性

How can i iterate through nested arrays of objects and remove some attribute

本文关键字:删除 属性 迭代 数组 何通过 对象 嵌套      更新时间:2023-09-26

我有一个JavaScript对象数组,每个对象都可以有子对象,子对象的类型与父对象的类型相同,子对象可以有多个子对象。我想遍历所有节点并改变一些值。

[
    {
        "text": "Auto",
        "icon": "/libs/jstree/folder.png",
        "state": {
            "opened": true,
            "selected": true
        }
    },
    {
        "text": "BookMark1",
        "icon": "/libs/jstree/folder.png",
        "state": {
            "opened": true
        },
        "children": [
            {
                "text": "BookMark2",
                "icon": "/libs/jstree/folder.png",
                "state": {
                    "opened": true
                }
            },
            {
                "text": "BookMark3",
                "icon": "/libs/jstree/folder.png",
                "state": {
                    "opened": true
                }
            },
            {
                "text": "BookMark4",
                "icon": "/libs/jstree/folder.png",
                "state": {
                    "opened": true
                }
            },
            {
                "text": "BookMark5",
                "icon": "/libs/jstree/folder.png",
                "state": {
                    "opened": true
                }
            },
            {
                "text": "BookMark6",
                "icon": "/libs/jstree/folder.png",
                "state": {
                    "opened": true
                },
                "children": [
                    {
                        "text": "BookMark2",
                        "icon": "/libs/jstree/folder.png",
                        "state": {
                            "opened": true
                        }
                    },
                    {
                        "text": "BookMark3",
                        "icon": "/libs/jstree/folder.png",
                        "state": {
                            "opened": true
                        }
                    }
                ]
            },
            {
                "text": "BookMark7",
                "icon": "/libs/jstree/folder.png",
                "state": {
                    "opened": true
                }
            }
        ]
    }
]

在上面的对象中,我想遍历所有节点并删除"state"属性。我怎样才能做到这一点呢?

这是递归的方法,它检查内部和外部。

https://jsfiddle.net/8ku7wohd/2/

function removeState(a){
    if (a.hasOwnProperty('state')) delete a['state'];
    if (a instanceof Array) a.forEach(removeState);
    else if (a instanceof Object) for (var k in a) removeState(a[k]);                 
}

试试这个:

function removeKeys(arr, key) {
    arr.forEach(function (element, index) {
        delete element[key];
        if (element.children !== undefined) removeKeys(element.children, key);
    });
}
removeKeys(json, "state");

小提琴:http://jsfiddle.net/9scd3qb3/

您需要在对象上使用递归函数进行迭代。我在这里为您创建了一个遍历嵌套对象的示例和最常见的方法:

function walk(item){
    for(index in item){
        console.log(item[index]);
        if(typeof item[index] == "object"){ 
            walk(item[index]); 
        }
    }
}

使用这个函数作为基础,你可以扩展它的功能,做任何你真正需要的。如果你使用的是JQuery,他们提供的功能.each(function(index, item){}),做同样的。

如果我可以在其他地方提供帮助,请在这里更新您的帖子或评论。

您可以使用JSON.stringifyreplacer参数这样做:

JSON.parse(JSON.stringify(object, ['text', 'icon', 'children']))

数组给出了一个"白名单"属性列表,这些属性将包含在结果中。所有其他属性(包括state)被忽略/删除

试一试:

jQuery.each(myJSONObject, function(i, val) {
    delete val.state;
});

小提琴更新:

另一个需要临时变量的解决方案。

var tempJsonObject = [];
jQuery.each(myJSONObject, function(i, val) {
    delete val.state;
    tempJsonObject[i] = val;
});
myJSONObject = tempJsonObject;

小提琴更新:

包括删除子对象的state属性。

removeProperty(myJSONObject, "state");
function removeProperty(myJSONObject, prop) {
    jQuery.each(myJSONObject, function(i, val) {
        delete val.state;
        if(val.hasOwnProperty("children")) {
          removeProperty(val.children, "state");
        }
    });
}

小提琴

var jsonObj = JSON.parse(text);
for (var i in jsonObj) {
   console.log(jsonObj[i].text);
   jsonObj[i].text = "some text"
   var innerJson = jsonObj[i].children
   for (var j in innerJson) {
       console.log(innerJson[j].text);
       innerJson[j].text = "some other text for all inner"
   }
};