underscorejs _find与对象数组

时间:2015-08-28 06:55:05

标签: json angularjs

我对下划线很新,我想在网上存储以下json文件中找到一个值(http://bacly.fr/baclymphp/poona.json

{
    "joueur": [
      {
        "civilite": "M.",
        "nom": "HURAT",
        "prenom": "Florent",
        "instance": "Badminton Club De Lyon",
        "licence": "00403566",
        "hebdoSimpleClassementId": "R5",
        "hebdoSimpleCote": "128.00",
        "hebdoDoubleClassementId": "R4",
        "hebdoDoubleCote": "475.98",
        "hebdoMixteClassementId": "R5",
        "hebdoMixteCote": "203.40"
      },
      {
        "civilite": "M.",
        "nom": "IHUEL",
        "prenom": "Philipe",
        "instance": "Badminton Club De Lyon",
        "licence": "06468814",
        "hebdoSimpleClassementId": "D7",
        "hebdoSimpleCote": "32.00",
        "hebdoDoubleClassementId": "R6",
        "hebdoDoubleCote": "78.29",
        "hebdoMixteClassementId": "R6",
        "hebdoMixteCote": "69.72"
      },
      {
        "civilite": "M.",
        "nom": "ISTRE",
        "prenom": "Jacques",
        "instance": "Badminton Club De Lyon",
        "licence": "00258046",
        "hebdoSimpleClassementId": "P2",
        "hebdoSimpleCote": "2.00",
        "hebdoDoubleClassementId": "P1",
        "hebdoDoubleCote": "4.74",
        "hebdoMixteClassementId": "P2",
        "hebdoMixteCote": "2.00"
      },
      {
        "civilite": "M.",
        "nom": "JACQUEMET",
        "prenom": "Mathieu",
        "instance": "Badminton Club De Lyon",
        "licence": "06910375",
        "hebdoSimpleClassementId": "P2",
        "hebdoSimpleCote": "2.70",
        "hebdoDoubleClassementId": "P3",
        "hebdoDoubleCote": "0.00",
        "hebdoMixteClassementId": "P3",
        "hebdoMixteCote": "0.00"
      },
      {
        "civilite": "Mlle",
        "nom": "JACQUEMIN",
        "prenom": "Alice",
        "instance": "Badminton Club De Lyon",
        "licence": "00273896",
        "hebdoSimpleClassementId": "R4",
        "hebdoSimpleCote": "385.83",
        "hebdoDoubleClassementId": "N3",
        "hebdoDoubleCote": "657.08",
        "hebdoMixteClassementId": "R4",
        "hebdoMixteCote": "300.00"
      }
    ]
}

我使用以下工厂继续:

baclyApp.factory('poonaFactory',function($http){
  var factory ={};
  $http.get("http://bacly.fr/baclymphp/readPoona.php").success(function(data)
  // $http.get("php/readPoona.php").success(function(data)
  {
    factory.joueurs = data;
    console.log(factory.joueurs);
    }).error(function(status) {
    alert("pas d accès réseau") 
    });

return {
        list: function(){
            return factory;
        },
        find: function(num){
            return _.find(factory.joueurs, function(item) {return item.licence == num});
        }       
        }
});

并且调用就像这样(调试目的我硬编码了这个值):

console.log(poonaFactory.find("06468814"));

欢迎任何建议! THX

1 个答案:

答案 0 :(得分:0)

您需要return _.find(factory.joueur, ...)而不是return _.find(factory.joueurs, ...)