[英]Iterate through layered objects in JavaScript
I have an object like this: 我有一个这样的对象:
var shoppers = {
"Bob": {favoriteApple: "Honeycrisp"},
"Sue": {favoriteApple: "Braeburn"},
"Derek": {favoriteApple: "Honeycrisp"}
}
I want to iterate through this object and count how many people prefer each kind of apple. 我想遍历此对象并计算有多少人喜欢每种苹果。 How can I do this?
我怎样才能做到这一点?
On a related note, why does this log as undefined
three times instead of logging the actual apple name? 在相关说明中,为什么将此日志记录为
undefined
三次,而不是记录实际的苹果名称?
for (var person in shoppers) {
console.log(person.favoriteApple); // undefined
}
You can use following 您可以使用以下
var shoppers = { "Bob": { favoriteApple: "Honeycrisp" }, "Sue": { favoriteApple: "Braeburn" }, "Derek": { favoriteApple: "Honeycrisp" } }; // Create empty object to store item count var countObj = {}; // Get the array of keys in object using `Object.keys` // Iterate over keys array using `forEach` Object.keys(shoppers).forEach(function(key) { // Get the value of favoriteApple var favApple = shoppers[key].favoriteApple; // If count exists, increment it else add count as 1 countObj[favApple] = countObj[favApple] ? countObj[favApple] + 1 : 1; }); console.log(countObj); document.getElementById('result').innerHTML = JSON.stringify(countObj, 0, 4);
<pre id="result"></pre>
Regarding the second question 关于第二个问题
why does
console.log(person.favoriteApple);
为什么
console.log(person.favoriteApple);
log as undefined three times instead of logging the actual apple name?记录为未定义的3次,而不是记录实际的苹果名称?
for (var person in shoppers) {
console.log(person.favoriteApple);
}
person
in the for
is the key of the object shoppers
. person
在for
是对象的关键shoppers
。 To access the actual value of the key use shoppers[person]
. 要访问密钥的实际值,请使用
shoppers[person]
。
Code: 码:
for (var key in shoppers) {
// To stop showing the inherited/prototyped properties
if (shoppers.hasOwnProperty(key)) {
console.log(shoppers[key].favoriteApple);
}
}
When you use 使用时
for (var prop in obj)
It iterates over keys of this object. 遍历此对象的键 。 So, in your case
person
will be Bob
, Sue
, Derek
. 因此,在您的情况下,该
person
将是Bob
, Sue
和Derek
。
All you need is to use this key to access a value of a property: 您需要使用此键来访问属性的值:
for (var person in shoppers)
{
console.log(shoppers[person]);
}
you can keep a tally like so: 您可以像这样保持统计:
var favoriteApples = {};
for(var name in shoppers) {
var person = shoppers[name];
if(!favoriteApples[person.favoriteApple]) {
favoriteApples[person.favoriteApple] = 1;
}else{
favoriteApples[person.favoriteApple]++;
}
}
console.log(favoriteApples);
// prints Object {Honeycrisp: 2, Braeburn: 1}
var shoppers = { "Bob": {favoriteApple: "Honeycrisp"}, "Sue": {favoriteApple: "Braeburn"}, "Derek": {favoriteApple: "Honeycrisp"} } $.each(shoppers,function(key,val){ console.log(key); $.each(val,function(k,v){ console.log(k + " " + v) }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.