jQuery - How to recursively loop over an object's nested properties?

后端 未结 5 1260
执笔经年
执笔经年 2020-12-16 20:46

Is there are a way to recursively loop over all the nested properties of a JS/jQuery object?

For example, given this object

var x = {
    \'name\': \         


        
5条回答
  •  抹茶落季
    2020-12-16 21:03

    A recursive approach seems best, something like this:

    function recursiveIteration(object) {
        for (var property in object) {
            if (object.hasOwnProperty(property)) {
                if (typeof object[property] == "object"){
                    recursiveIteration(object[property]);
                }else{
                    //found a property which is not an object, check for your conditions here
                }
            }
        }
    }
    

    This is a working fiddle

提交回复
热议问题