Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

How to Iterate Map that contain list in the jquery function

Please help me how to iterate the following in jquery.

var data={100=[submenu 4, submenu 5, submenu 6],
          120=[submenu 10, submenu 11, submenu 12],
          125=[submenu 19, submenu 20, submenu 21],
          99=[submenu 16, submenu 17, submenu 18], 
          77=[submenu 13, submenu 14, submenu 15],
          70=[submenu 1, submenu 2, submenu 3],
          44=[submenu 7, submenu 8, submenu 9]};

I want to iterate the above one based on the ids.....100,120,125............

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

First, check if your data object is correct:

var data = { "100" : ["submenu 4", "submenu 5", "submenu 6"],
             "120" : ["submenu 10", "submenu 11", "submenu 12"],
             "125" : ["submenu 19", "submenu 20", "submenu 21"],
             "99"  : ["submenu 16", "submenu 17", "submenu 18"], 
             "77"  : ["submenu 13", "submenu 14", "submenu 15"],
             "70"  : ["submenu 1", "submenu 2", "submenu 3"],
             "44"  : ["submenu 7", "submenu 8", "submenu 9"] };

Then you may use for loop to iterate elements in the object:

for (var key in data) {
    console.log(data[key]);
}

You may also use jQuery $.each() method:

$.each(data, function(key, value) {
    console.log(key);
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...