我有这个JSON
{
"_id": "586baa68734d1d7bf4f06432",
"topic": 1,
"story": {
"2016-01-01": 152,
"2016-01-02": 203,
"2016-01-03": 419,
"2016-01-04": 792,
"2016-01-05": 457,
"2016-01-06": 350,
"2016-01-07": 666
}
}
此函数打印出此JSON
exports.getData = function(req, res){
Top10.find({},function(err, docs){
console.log("docs "+docs);
res.json(docs);
});
}
现在我想获得特定日期的NUMBER - 让我们说2016-01-01我想回来152
我该怎么做?
答案 0 :(得分:2)
要访问嵌套元素,请尝试以下操作:
var date = "2016-01-01";
var number = docs["story"][date];
如果docs是一个数组(访问第一个元素):
var date = "2016-01-01";
var numbers = [];
for(var i = 0; i < docs.length; i++) {
if(docs[i]["story"][date]) {
numbers.push(docs[i]["story"][date]);
}
}
console.log(numbers);