正确循环JSON对象的方法

时间:2016-04-17 09:37:05

标签: json node.js mongodb object

Since there seems nobody is able to find a solution for my problem,我想以不同的方式提出我的问题: 循环通过以下对象的游戏部分的正确方法应该是什么:

[
    {
        "_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"
                }
            ]
        }
    }
]

这个对象是通过MongoDB中的mongoose获取的,所有这些都发生在我的NodeJS应用程序的API部分:

.get('/test', function(req, res){
    User.findOne({"username": req.decoded.username}, function(err, user){

        // trying to loop here

    });
})

我已经尝试了所有我找到的东西,但是我无法让它发挥作用,所以我想知道你用什么方法来做这件事,而不会因为我的(可能是错误的)方式和错误而使你的视线混乱......

2 个答案:

答案 0 :(得分:2)

更新答案:

与原始问题相比,您的编辑显着会更改数据结构。

现在,您有一个数组,其中包含一个library的条目,其games,因此您引用它(假设user指向整个事情)为user[0].library.games。 E.g:

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);
}




原始答案:

games是一个数组,假设您o引用该对象,您可以从o.user.library.games引用它。

有很多方法可以循环遍历数组(有关列表,请参阅this answer);其中一个是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);
}




从下面的评论中,听起来结构可能不像你在问题中引用的那样。您可以使用node-inspector调试NodeJS代码,在要循环的位置开始设置断点,并检查引用该对象的变量。那会告诉你它的结构。它非常容易安装(npm install -g node-inspector)并使用(node-debug your-main-file.js)。

答案 1 :(得分:2)

用于... in循环Javascript / JSON对象

for (variable in object) {...
}

for...in documentation

使用forEach循环遍历Javascript / JSON数组

arr.forEach(callback[, thisArg])

forEach documentation

相关问题