[英]Loop through javascript object and get specific value from given key
I have a function within my project that loops through a team of people and adds them to an object: 我的项目中有一个函数循环遍历一组人并将它们添加到一个对象:
var trainingTeam = {};
...
$(data).find('trainingTeam>member').each(function() {
trainingTeam = {first: tFirst, last: tLast, ntid: tNTID, empID: tEmp, role: tRole};
}
I am now trying to loop over that object and get specific values from it. 我现在正试图遍历该对象并从中获取特定值。
$.each(trainingTeam, function(x, y) {
alert('Team Member : ' y.first + ' ' + y.last)
});
This returns undefined with both X
and Y
so I am obviously doing something wrong. 这与
X
和Y
都返回undefined,所以我显然做错了。
Am I close? 我接近了吗?
UPDATE UPDATE
Solved by changing the var to var trainingTeam = []
通过将var更改为
var trainingTeam = []
I then pushed the object to the array in the loop : 然后我将对象推送到循环中的数组:
trainingTeam.push({first: tFirst, last: tLast, ntid: tNTID, empID: tEmp, role: tRole});
X
and Y
, are key
and value
. X
和Y
是key
和value
。
So 所以
$.each(trainingTeam, function(x, y) {
//here y is 'first' and x is 'tFirst'
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.