Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'm trying to search inside JSON file by the _id, then only return the team_members array as you see below is an example of one object inside the file

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

and here my code:

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;
};
share|improve this question
1  
I dont think that is possible, try itrating over the array - stackoverflow.com/questions/19590063/… – Itsik Mauyhas Jun 22 at 12:30
    
_.find works on array, not on object – Pankaj Parkar Jun 22 at 12:31
    
database.products is an array of product objects ? – gaurav5430 Jun 22 at 12:38
    
@gaurav5430 yes it's an array. – csbukhari Jun 22 at 12:56
    
@PankajParkar actually the files content an array of products – csbukhari Jun 22 at 12:56
up vote 1 down vote accepted

Here's a working plunk. Since $http is promise based you can remove the dfd promise and return it directly. Then handle the promise resolution from your calling func.

Controller

  var self = this;

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

  // this could be wrapped in a func that is called from your html
  this.getProductReferences("5769484bb3ef5c696c5686d0").then(function(result) {
    self.references = result.data.team_members;
  })

I chose to pass "5769484bb3ef5c696c5686d0" in as productId just for this example

share|improve this answer

You can use $filter service.

var object = $filter('filter')(database, {'_id': '5769484bb3ef5c696c5686d0'});
console.log(object.team_members) // here is your team members array
share|improve this answer
    
filter works on array, not sure if database is an array – gaurav5430 Jun 22 at 12:38
    
He wrote that the example is an object inside the file. So I thought there should be many objects in an array in this json file. – yogurt Jun 22 at 12:43
    
in that case his own code should work as well (with some modifications maybe) – gaurav5430 Jun 22 at 12:44
    
@yogurt thanks your solution also work ... – csbukhari Jun 22 at 13:24

Thanks all, it's working with me know, some one write the answer and then delete and i don't know why but i try before and it's work..

  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;
 });
share|improve this answer
    
I had deleted it since I needed to make a change but couldn't get to it right away. I've undeleted it now. – jbrown Jun 22 at 13:41
    
@jbrown thanks a lot – csbukhari Jun 22 at 19:36
    
You're welcome. – jbrown Jun 22 at 19:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.