AngularJS:在Object中返回一个数组

时间:2016-06-22 12:20:41

标签: angularjs ionic-framework

我试图通过_id在JSON文件中搜索,然后只返回team_members数组,如下所示是文件中一个对象的示例

InputMethodManager.SHOW_FORCED
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

这里是我的代码:

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

3 个答案:

答案 0 :(得分:1)

这是一个有效的plunk。由于$ http是基于promise的,因此您可以删除dfd promise并直接返回。然后从你的调用函数处理promise promise。

<强>控制器

for (var key in data.VALUE.USER24) {
  if(data.VALUE.USER24.hasOwnProperty(key)){
  console.log('key:' + key + ', val:' + data.VALUE.USER24[key]);
  }
}

我选择将“5769484bb3ef5c696c5686d0”作为productId传递给此示例

答案 1 :(得分:0)

您可以使用$filter服务。

var object = $filter('filter')(database, {'_id': '5769484bb3ef5c696c5686d0'});
console.log(object.team_members) // here is your team members array

答案 2 :(得分:0)

谢谢大家,它和我一起工作,知道, 有人写了答案,然后删除,我不知道为什么,但我以前尝试过它的工作..

  this.getProductReferences = function(productId) {
   return $http.get('database.json').success(function(database) {
     var product = _.find(database.products, function(product) {
       return product._id == productId;
     });
     return product.team_members;
   });
 };

 this.getProductReferences(productId).then(function(references) {
  $scope.references = references.data.team_members;
 });