Arrays 如何在JSON中使用find
我有这个JSONArrays 如何在JSON中使用find,arrays,json,node.js,Arrays,Json,Node.js,我有这个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,
{
"_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
}
}
exports.getData = function(req, res){
Top10.find({},function(err, docs){
console.log("docs "+docs);
res.json(docs);
});
}
此函数用于打印此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
}
}
exports.getData = function(req, res){
Top10.find({},function(err, docs){
console.log("docs "+docs);
res.json(docs);
});
}
现在我想得到一个特定日期的号码——比如说2016-01-01,我想回去
我该怎么做
要访问嵌套元素,请尝试以下操作:
var date = "2016-01-01";
var number = docs["story"][date];
如果docs是数组(用于访问第一个元素):
var date=“2016-01-01”;
var数=[];
对于(变量i=0;i
docs[“story”][“2016-01-01”]
@JonasKöritz感谢您的回答!我试过console.log(“docs”+docs[“story”]['2016-01-01']);它仍然不起作用,文件里面是什么?可以发布docs变量的转储吗?所以docs是一个数组,我的第二个代码块应该可以工作。如果你想要数组中的所有项目,你必须循环遍历docs数组。@JonasKöritz我们能得到数组中的所有项目吗?@JonasKöritz我尝试了你的代码,但它给我写了一个错误:“无法读取unfind的属性'2016-01-01'@JonasKöritz yee找到你了。”