简体   繁体   English

遍历JavaScript中的分层对象

[英]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 您可以使用以下

  1. Iterate over the keys of main object by using Object.keys and forEach 使用Object.keysforEach遍历主对象的键
  2. Create an empty object to store the various items with count 创建一个空对象以存储带有计数的各种项目
  3. Inside loop, check if the item is already present in the countObj, if yes then increment the count, else set the count as 1 在循环中,检查countObj中是否已存在该项目,如果是,则增加计数,否则将计数设置为1

 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 . personfor是对象的关键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将是BobSueDerek

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM