简体   繁体   English

如何使用javascript仅迭代嵌套对象的顶级键?

[英]How do I iterate through only the top level keys of a nested object with javascript?

For example, if I were to iterate through the following object:例如,如果我要遍历以下对象:

{
  "Crows": {
    "players": {
      "Ben": {
        "jersey #": "1B"
      },
      "Ty": {
        "jersey #": "2B"
      }
    }
  },
  "Pigeons": {
    "players": {
      "Bill": {
        "jersey #": "1B"
      },
      "Tim": {
        "jersey #": "2B"
      }
    }
  },
  "Seagulls": {
    "players": {
      "Bob": {
        "jersey #": "1B"
      },
      "Tom": {
        "jersey #": "2B"
      }
    }
  }
}

I want to only iterate and manipulate the top level keys, which in this case are "Crows", "Pigeons", and "Seagulls".我只想迭代和操作顶级键,在这种情况下是“Crows”、“Pigeons”和“Seagulls”。

When I try to iterate with a traditional for (var key in data) loop, a console.log of the keys gives me every key in the object, including the nested ones like "players".当我尝试使用传统的for (var key in data)循环进行迭代时, for (var key in data)的 console.log 为我提供了对象中的每个键,包括像“玩家”这样的嵌套键。 How would I only loop through parent keys?我将如何只循环访问父键?

First, JSON is a string format for transporting data over HTTP/HTTPS.首先,JSON 是一种字符串格式,用于通过 HTTP/HTTPS 传输数据。 You don't iterate JSON, you parse it back into a regular JavaScript object and then you do whatever you like with that object.您不迭代 JSON,而是将其解析回常规 JavaScript 对象,然后对该对象执行任何您喜欢的操作。 What you've posted is just a plain JavaScript object, so it looks like you've already done that parsing.你发布的只是一个普通的 JavaScript 对象,所以看起来你已经完成了解析。

So, in your case:所以,在你的情况下:

 let result = { "Crows": { "players": { "Ben": { "jersey #": "1B" }, "Ty": { "jersey #": "2B" } } }, "Pigeons": { "players": { "Bill": { "jersey #": "1B" }, "Tim": { "jersey #": "2B" } } }, "Seagulls": { "players": { "Bob": { "jersey #": "1B" }, "Tom": { "jersey #": "2B" } } } }; for(var prop in result){ console.log(prop); // Just get the property name only console.log(result[prop]); // Or, get the data held in that property // Or, dig deeper still. Either of the following syntaxes will work: console.log(result[prop]["players"]); console.log(result[prop].players); }

Either use a for...in loop:要么使用for...in循环:

 const data = { "Crows": { "players": { "Ben": { "jersey #": "1B" }, "Ty": { "jersey #": "2B" } } }, "Pigeons": { "players": { "Bill": { "jersey #": "1B" }, "Tim": { "jersey #": "2B" } } }, "Seagulls": { "players": { "Bob": { "jersey #": "1B" }, "Tom": { "jersey #": "2B" } } } } for (let key in data) { console.log(key); }

Or collect them into one array:或者将它们收集到一个数组中:

 const data = { "Crows": { "players": { "Ben": { "jersey #": "1B" }, "Ty": { "jersey #": "2B" } } }, "Pigeons": { "players": { "Bill": { "jersey #": "1B" }, "Tim": { "jersey #": "2B" } } }, "Seagulls": { "players": { "Bob": { "jersey #": "1B" }, "Tom": { "jersey #": "2B" } } } } console.log(Object.keys(data));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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