2016-04-23 25 views
1

我想使用underscorejs从ajax响应中筛选出以后将由我的angualr js应用程序使用的对象。以下是来自服务器(对象数组)的响应。如何查找具有复杂/嵌套属性的json对象数组的特定记录

[ 
    { 
     "description": "squash", 
     "image": "/categories/8/image", 
     "category": "squash", 
     "city": "Dublin", 
     "dateFrom": "2015-12-12 01:01:01", 
     "dateTo": "2016-12-12 01:01:01", 
     "others": "dublin squash", 
     "users": [ 
     { 
      "image": "https://stackoverflow.com/users/2/image", 
      "username": "faisal", 
      "id": 2 
     }, 
     { 
      "image": "https://stackoverflow.com/users/3/image", 
      "username": "zaid", 
      "id": 3 
     } 
     ], 
     "address": "Dublin", 
     "name": "Squash in dublin", 
     "id": 5, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "Tennis event in Dublin", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "Dublin", 
     "dateFrom": "2015-12-12 01:01:01", 
     "dateTo": "2016-12-12 01:01:01", 
     "others": "None", 
     "users": [ 
     { 
      "image": "https://stackoverflow.com/users/3/image", 
      "username": "zaid", 
      "id": 3 
     }, 
     { 
      "username": "[email protected]", 
      "id": 1 
     }, 
     { 
      "image": "https://stackoverflow.com/users/2/image", 
      "username": "faisal", 
      "id": 2 
     } 
     ], 
     "address": "RIchmond road, Dublin 3", 
     "name": "Tennis in Dublin", 
     "id": 6, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "tennis in karachi", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "Karachi", 
     "dateFrom": "2016-03-03 00:00:00", 
     "dateTo": "2017-03-03 00:00:00", 
     "others": "none", 
     "users": [], 
     "address": "Karachi", 
     "name": "Tennis in karachi", 
     "id": 7, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "tennis in karachi", 
     "image": "/categories/1/image", 
     "category": "SPORTS", 
     "city": "Karachi", 
     "dateFrom": "2016-03-03 00:00:00", 
     "dateTo": "2017-03-03 00:00:00", 
     "others": "none", 
     "users": [], 
     "address": "Karachi", 
     "name": "Tennis in karachi", 
     "id": 8, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "tennis in KHIIII", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "Karachi", 
     "dateFrom": "2016-05-05 01:01:01", 
     "dateTo": "2017-05-05 01:01:01", 
     "others": "sd", 
     "users": [], 
     "address": "KHIIII", 
     "name": "Tennis in KHIIIII", 
     "id": 10, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 123", 
     "id": 12, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test12345", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1235", 
     "id": 13, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test12346", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1236", 
     "id": 14, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test12347", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1237", 
     "id": 15, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test12348", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238", 
     "id": 16, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123481", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 12381", 
     "id": 17, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123482", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 12382", 
     "id": 18, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123483", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 12383", 
     "id": 19, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123484", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 12384", 
     "id": 20, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123485", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 12385", 
     "id": 21, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123486", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 12386", 
     "id": 22, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123487", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 12387", 
     "id": 23, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123488", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 12388", 
     "id": 24, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test123489", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 123889", 
     "id": 25, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234891", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238891", 
     "id": 26, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234892", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238892", 
     "id": 27, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234893", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238893", 
     "id": 28, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234894", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238894", 
     "id": 29, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234895", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238895", 
     "id": 30, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234896", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238896", 
     "id": 31, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234897", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238897", 
     "id": 32, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234898", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238898", 
     "id": 33, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234898", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238898", 
     "id": 34, 
     "country": "PAKISTAN" 
    }, 
    { 
     "description": "test1234899", 
     "image": "/categories/2/image", 
     "category": "CRICKET", 
     "city": "dublin", 
     "dateFrom": "2016-12-12 01:01:01", 
     "dateTo": "2017-12-12 01:01:01", 
     "others": "none", 
     "users": [], 
     "address": "dublin", 
     "name": "test interest 1238899", 
     "id": 35, 
     "country": "PAKISTAN" 
    } 
    ]; 

array中的每个对象都是一个事件,其中有一些用户连接到它。我在我的角度应用程序中有两个按钮来显示所有事件,并且只显示事件的用户属性与当前用户匹配的事件。因此,例如当前用户有ID 2.然后,当他点击我的事件按钮时,应该只列出2个事件。

我正在angualr js过滤器中工作,它与ng-repeat链接,让我在这里分享。

iFinder.filter('myevent', function (iFinderFactory) { 

    return function (event) { 
     console.log("filter in action", iFinderFactory); 
     var user = iFinderFactory.getUser(); 
     console.log("filter user is =", user); 
     //console.log("underscore = ", underscore); 
     //console.log("evenst:"); 
     //console.log(event); 
     //var eUsers = event.users || []; 
     //if (eUsers && eUsers.length > 0) { 
     // console.log(event); 
     // return event; 
     //} 
     var myEvents = []; 
     var gg = _.filter(event, function (e) { 
      return _.where(e.users, { id: 3 }); 
      //return e.users.length > 0; 
      //_.each(e.users,function (el, index, list) { 
      // console.log("single e:", el); 
      // if (el.id == user.Id) { 
      //  myEvents.push(e); 

      // } 
      // else { 

      // } 
      //}); 
      //window.najams = myEvents; 
      //return myEvents; 
     }); 
     event = gg; 
     return event; 


    }; 
}); 

伍重复

<div class="result" ng-repeat="r in results | myevent"> 
<h3>{{r.name}}</h3> 
</div> 

使用underscore.js fitler方法我能显示所有谁拥有与他们的注册用户使用下面的代码的事件。但不能根据用户数组查找事件本身,而不能查找用户id = 3的事件。请帮助

iFinder.filter('myevent', function (iFinderFactory) { 

    return function (event) { 
     console.log("filter in action", iFinderFactory); 
     var user = iFinderFactory.getUser(); 
     console.log("filter user is =", user); 


     var gg = _.filter(event, function (e) { 
      return e.users.length > 0;    
     }); 
     event = gg; 
     return event; 
    }; 
}); 

回答

0

你的过滤器应该是这样的:

iFinder.filter('myevent', function() { 
    return function (event) { 
     var myEvents = []; 
     console.log("filter in action", iFinderFactory); 
     var user = iFinderFactory.getUser(); 
     console.log("filter user is =", user); 
     //assuming user.id is the id of the user in event.users 
     var filtered = _.filter(event, function (e) { 
      return _.where(e.users, { id: user.id }).length > 0; 
     }); 
     return filtered; 
    }; 
}); 

我试图做一个工作捣鼓你here

相关问题