简体   繁体   English

遍历Javascript中的三重嵌套对象

[英]Looping through a triple nested object in Javascript

I have declared a triple nested object, like so: 我已经声明了一个三层嵌套对象,如下所示:

var food = {
    "fruit": {
        "apples": {
            "redApples": 20,
            "greenApples": 30
        },
        "bananas": {
            "yellowBananas": 10
        }
    },
    "grains": {
        "bread": {
            "brownBread": 50
        }
    }
};

I found this question , which worked for iterating through an object of an object, but I'm stuck. 我发现了这个问题该问题可用于遍历一个对象的一个​​对象,但我遇到了麻烦。 How would I iterate through this object? 我将如何遍历此对象?

Update: For this specific problem, nested for-loops would work fine, like this: 更新:对于此特定问题,嵌套的for循环可以正常工作,如下所示:

for(var key in food)
    for(var prop in food[key])
        for(var foo in food[key][prop])
            console.log(food[key][prop][foo]);

However, there are some good recursive functions below which would do this for indefinitely nested objects. 但是,下面有一些不错的递归函数可以对无限嵌套的对象执行此操作。

Martin is right, recursive function is the approach. Martin是正确的,递归函数是方法。 Your function should check is the attribute an object. 您的函数应该检查的是对象的属性。 If it is - function must walk deeper by calling itself also, but with a nested object. 如果是-函数也必须通过调用自身来更深入,但要使用嵌套对象。

function recursiveIter(obj){
    for (var i in obj){
        if (typeof obj[i]=="object"){
            recursiveIter(obj[i]);
        }
        else {
            $('.ins').append(obj[i]);  // or what do you want to do with that           
        }
    }
}

recursiveIter(food);

jsfiddle with this approach. jsfiddle使用这种方法。

You can create a general function which accepts an object and a callback then does a recursive call. 您可以创建一个接受对象和回调的通用函数,然后进行递归调用。

function objectWalk (obj, callback){
    for(o in obj){
        if(typeof obj[o] === 'object'){
            objectWalk(obj[o], callback);
        }else {
            callback(obj[o]);
        }
    }
}

What I'm doing here is looping through the object and checking if each item in the loop is an object. 我在这里所做的是遍历对象,并检查循环中的每个项目是否都是对象。 If it is, call the same function again. 如果是,请再次调用相同的函数。 If not, pass the value of the object into a callback. 如果不是,请将对象的值传递给回调。 Its a handy little function. 它是一个方便的小功能。

See it in action here . 看到它在这里行动。

Like this? 像这样?

walk = function(obj, fn, key) {
    if(typeof obj != "object")
        return fn(obj, key)
    for(var p in obj)
        if(obj.hasOwnProperty(p))
            walk(obj[p], fn, p)
}

or in a more concise way with ES5 functions: 或以更简洁的方式使用ES5功能:

walk = function(obj, fn, key) {
    return obj === Object(obj) ? 
        Object.keys(obj).forEach(function(p) { walk(obj[p], fn, p) }) : 
        fn(obj, key);
}

Example: 例:

var food = {
    "fruit": {
        "apples": {
            "redApples": 20,
            "greenApples": 30
        },
        "bananas": {
            "yellowBananas": 10
        }
    },
    "grains": {
        "bread": {
            "brownBread": 50
        }
    }
};

walk(food, console.log.bind(console))

Result: 结果:

20 "redApples"
30 "greenApples"
10 "yellowBananas"
50 "brownBread"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM