[英]Loop ONLY through a defined JSON object in a nested structure - JQUERY
I have the following json structure (I've made it shorter for simplify): 我有以下json结构(为简化起见,我将其缩短了):
{
"employees":{"0":"Name1 Surname1", "1":"Name2 Surname2"},
"managers":{"0":"Name3 Surname3", "1":"Name4 Surname4"},
"teamleaders":{"0":"Name5 Surname5", "1":"Name6 Surname6"},
}
How can I loop ONLY through employees with JQuery? 我如何仅使用JQuery遍历员工? The below code gives me the whole 3 objects and I can't seem to be able to sort them according to the object name:
下面的代码为我提供了全部3个对象,但我似乎无法根据对象名称对它们进行排序:
$.each(item, function (i, item) {
$.each(item, function (key, value) {
//gives me ALL names and surnames
console.log(value);
})
});
Instead of iterating the outer object, item
, do like this. 像这样,而不是迭代外部对象
item
。
var item = {
"employees":{"0":"Name1 Surname1", "1":"Name2 Surname2"},
"managers":{"0":"Name3 Surname3", "1":"Name4 Surname4"},
"teamleaders":{"0":"Name5 Surname5", "1":"Name6 Surname6"},
}
$.each(item.employees, function(key, value) { ... });
You can use plain JS for this. 您可以为此使用普通的JS。
for (var employee in json.employees) {
if (json.employees.hasownProperty(employee))
console.log(json.employees[employee])
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.