Javascript For each json
This question already has an answer here:
var obj = {"options": {"1": "All locations", "2": "Egypt", "3": "El Alamein", "4": "Marsa matrouh", "5": "Sharm el sheikh "}}
for(var item in obj.options) {
console.log(obj.options[item]);
}
For iterating over dictionary you can sue this:
var dict = obj.options;
Object.keys(dict).forEach(function(key) {
console.log(key +': '+ dict[key]);
});
to know only the length you can simply use:
Object.keys(dict).length;
I think that here you can find more answers: How do I loop through or enumerate a JavaScript object?
使用for in
循环
var obj = {"options": {"1": "All locations", "2": "Egypt", "3": "El Alamein", "4": "Marsa matrouh", "5": "Sharm el sheikh "}};
var ul = document.querySelector('ul');
for (var index in obj.options) {
var li = document.createElement('li')
ul.appendChild(li);
li.innerHTML = index + ": " + obj.options[index];
}
<ul ></ul>
链接地址: http://www.djcxy.com/p/28832.html
上一篇: 使用for / in循环javascript显示对象属性
下一篇: Javascript对于每个json