node.js 循环遍历 JSON 对象的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36674884/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to loop through an JSON object
提问by stijnpiron
Since there seems nobody is able to find a solution for my problem, I would like to ask my question in a different way: What should be the correct way to loop through the games section of the following object:
由于似乎没有人能够为我的问题找到解决方案,我想以不同的方式提出我的问题:循环遍历以下对象的游戏部分的正确方法是什么:
[
{
"_id": "5710b0ddab8b724011705037",
"username": "test",
"name": "testuser",
"__v": 0,
"library": {
"games": [
{
"platform": [
"17",
"94"
],
"name": "Simcity",
"id": "37620"
},
{
"platform": [
"146",
"20"
],
"name": "destiny",
"id": "36067"
}
],
"platforms": [
{
"name": "Xbox360",
"id": "20"
},
{
"name": "PC",
"id": "94"
}
]
}
}
]
This object is fetched out through mongoose out of a MongoDB, all happens in the API section of my NodeJS application:
这个对象是通过 mongoose 从 MongoDB 中取出的,所有这些都发生在我的 NodeJS 应用程序的 API 部分:
.get('/test', function(req, res){
User.findOne({"username": req.decoded.username}, function(err, user){
// trying to loop here
});
})
I have tried everything I find, but I cannot get it to work, so I wonder what ways you use to do this, without cluttering your vision with my (maybe wrong) ways and errors...
我已经尝试了我找到的所有方法,但我无法让它工作,所以我想知道你用什么方法来做到这一点,而不用我的(可能是错误的)方法和错误来扰乱你的视野......
回答by T.J. Crowder
Updated Answer:
更新答案:
Your edit markedlychanges the structure of the data compared to your original question.
与原始问题相比,您的编辑显着改变了数据的结构。
Now, you have an arraycontaining one entry, which has library, which has games, so you reference it (assuming userpoints to the whole thing) as user[0].library.games. E.g.:
现在,您有一个包含一个条目的数组,其中有library,有games,因此您将它(假设user指向整个事物)引用为user[0].library.games。例如:
user[0].library.games.forEach(function(entry) {
// Use `entry` here
});
var user = [{
"_id": "5710b0ddab8b724011705037",
"username": "test",
"name": "testuser",
"__v": 0,
"library": {
"games": [{
"platform": [
"17",
"94"
],
"name": "Simcity",
"id": "37620"
}, {
"platform": [
"146",
"20"
],
"name": "destiny",
"id": "36067"
}],
"platforms": [{
"name": "Xbox360",
"id": "20"
}, {
"name": "PC",
"id": "94"
}]
}
}];
user[0].library.games.forEach(function(entry) {
log(entry.name);
});
function log(msg) {
var p = document.createElement('p');
p.appendChild(document.createTextNode(msg));
document.body.appendChild(p);
}
Original Answer:
原答案:
gamesis an array, assuming you have oreferencing that object, you can reference it from o.user.library.games.
games是一个数组,假设您o引用了该对象,则可以从o.user.library.games.
There are lots of ways to loop over arrays (see this answerfor a list); one of them is forEach:
有很多方法可以循环遍历数组(请参阅此答案以获取列表);其中之一是forEach:
o.user.library.games.forEach(function(entry) {
// Use `entry` here, e.g. `entry.name`, `entry.id`, etc.
});
E.g.:
例如:
var o = {
"user": {
"name": "testuser",
"library": {
"platforms": [{
"id": "20",
"name": "Xbox360"
}, {
"id": "94",
"name": "PC"
}],
"games": [{
"id": "37620",
"name": "Simcity",
"platform": [
"17",
"94"
]
}, {
"id": "36067",
"name": "destiny",
"platform": [
"146",
"Xbox360"
]
}]
}
}
};
o.user.library.games.forEach(function(entry) {
log(entry.name);
});
function log(msg) {
var p = document.createElement('p');
p.appendChild(document.createTextNode(msg));
document.body.appendChild(p);
}
From your comment below, it sounds like the structure may not be quite as you quoted it in the question. You can use node-inspectorto debug your NodeJS code, set a breakpoint at the beginning of where you want to loop, and inspect the variable you have referring to the object. That will show you its structure. It's very easy to install (npm install -g node-inspector) and use (node-debug your-main-file.js).
从您下面的评论中,听起来结构可能与您在问题中引用的不太一样。你可以node-inspector用来调试你的 NodeJS 代码,在你想要循环的开始处设置一个断点,并检查你所引用的对象的变量。这将向您展示它的结构。安装 ( npm install -g node-inspector) 和使用 ( node-debug your-main-file.js)非常容易。
回答by shikhar
Use for...in to loop through Javascript/JSON objects
使用 for...in 循环遍历 Javascript/JSON 对象
for (variable in object) {...
}
Use forEach to loop through Javascript/JSON arrays
使用 forEach 循环遍历 Javascript/JSON 数组
arr.forEach(callback[, thisArg])

