【发布时间】:2016-10-24 09:15:45
【问题描述】:
我正在尝试通过 _id 在 JSON 文件中搜索,然后只返回 team_members 数组,如下所示是文件中一个对象的示例
{
"_id": "5769484bb3ef5c696c5686d0",
"name": "XXXXX",
"family": "XXXXX Products",
"description": "XXXXXXXXXXXXXXXX",
"image": "http://localhost/img/proudct6.png",
"__v": 8,
"team_members": [{
"_id": "57694567b3ef5c696c5686c2",
"name": "XXXXXXX",
"description": "team member",
"image": "http://localhost/img/1.png",
"mobile_number": "XXXXXXXXXXXXXXX",
"specialty": "Developer",
"summary": "only for test"
}, {
"_id": "57694567b3ef5c678c5686c6",
"name": "XXXXXXX",
"description": "team member",
"image": "http://localhost/img/1.png",
"mobile_number": "XXXXXXXXXXXXXXX",
"specialty": "Developer",
"summary": "only for test"
}]
}
这里是我的代码:
this.getProductReferences = function(productId){
var dfd = $q.defer();
$http.get('database.json').success(function(database) {
var product = _.find(database.products, function(product){ return product._id == productId; });
var references =product.references;
dfd.resolve(references);
});
return dfd.promise;
};
【问题讨论】:
-
我不认为这是可能的,尝试遍历数组 - stackoverflow.com/questions/19590063/…
-
_.find作用于数组,而不作用于对象 -
database.products 是一个产品对象数组?
-
@gaurav5430 是的,它是一个数组。
-
@PankajParkar 实际上文件内容是一系列产品