最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop through nested objects with jQuery - Stack Overflow

programmeradmin2浏览0评论

Hey everyone I am trying t find the most dynamic way to loop through an array and return specific values return specific values... The json is deeply structured and may change, could there be a $.each() formula that can help?

Example:

var myobj = {
    obj1: { key1: 'val1', key2: 'val2' },
    obj2: { key1: '2val1', 
           key2: { nest1: 'val1', nest2: 'val2', nest3: 'val3' }, 
           key3: { nest1: 'K3val1', nest2: 'K3val2', 
                 nest3: [
                         { nest1: 'val1', nest2: 'val2', nest3: 'val3' }, 
                         { nest1: 'val1', nest2: 'val2', nest3: 'val3' }
                        ]
                 }
          },
    obj3: { key1: 'dddddval1', key2: 'val2' }
    }

now lets say i want to retrieve "K3val2" value but instead of hardcoding it like so: myobj.obj2.key3.nest2 is there a dynamic way I do this with $.each() mybe?

Hey everyone I am trying t find the most dynamic way to loop through an array and return specific values return specific values... The json is deeply structured and may change, could there be a $.each() formula that can help?

Example:

var myobj = {
    obj1: { key1: 'val1', key2: 'val2' },
    obj2: { key1: '2val1', 
           key2: { nest1: 'val1', nest2: 'val2', nest3: 'val3' }, 
           key3: { nest1: 'K3val1', nest2: 'K3val2', 
                 nest3: [
                         { nest1: 'val1', nest2: 'val2', nest3: 'val3' }, 
                         { nest1: 'val1', nest2: 'val2', nest3: 'val3' }
                        ]
                 }
          },
    obj3: { key1: 'dddddval1', key2: 'val2' }
    }

now lets say i want to retrieve "K3val2" value but instead of hardcoding it like so: myobj.obj2.key3.nest2 is there a dynamic way I do this with $.each() mybe?

Share Improve this question edited Jul 9, 2013 at 11:41 a better oliver 26.8k2 gold badges62 silver badges66 bronze badges asked Jul 9, 2013 at 11:11 Simo MafuxwanaSimo Mafuxwana 3,7627 gold badges44 silver badges59 bronze badges 5
  • 5 Keyword: "recursion". Now it's time to google – zerkms Commented Jul 9, 2013 at 11:12
  • That's not an array, it's an object with a bunch of nested objects. (It contains one array, the one assigned to nest3.) (It's also not JSON, as tagged.) – T.J. Crowder Commented Jul 9, 2013 at 11:13
  • 2 "deeply structured and may change," - So how do you know which key to even look for? The only example you gave is asking how to find a specific value not the value associated with a specific key so...? – nnnnnn Commented Jul 9, 2013 at 11:24
  • How do you want to "retrieve" the value if you don't know where it is located, how do you recognize that you found it? If you wanted to search for it you would know the value already… – Bergi Commented Jul 9, 2013 at 11:45
  • @nnnnnn that will depend on the specific value I have to pull out at that specific time, which means I will hook that in a trigger.. – Simo Mafuxwana Commented Jul 9, 2013 at 12:11
Add a comment  | 

1 Answer 1

Reset to default 19

You can simply nest calls to $.each:

Live Example | Live Source

// Loop the top level
$.each(myobj, walker);

function walker(key, value) {
    // ...do what you like with `key` and `value`

    if (value !== null && typeof value === "object") {
        // Recurse into children
        $.each(value, walker);
    }
}

If you want to know how deep you are, you can do that too:

Live Example | Live Source

var path = "";

// Loop the top level
$.each(myobj, walker);

function walker(key, value) {
    var savepath = path;

    path = path ? (path + "." + key) : key;

    // ...do what you like with `key` and `value`

    if (value !== null && typeof value === "object") {
        // Recurse into children
        $.each(value, walker);
    }

    path = savepath;
}
发布评论

评论列表(0)

  1. 暂无评论