如何针对具有复杂/嵌套属性的json对象数组查找特定记录

时间:2016-04-23 15:33:59

标签: javascript angularjs underscore.js

我正在尝试使用underscorejs来过滤掉ajax响应中的对象,以后我的angualr js app会使用它。以下是服务器(对象数组)的响应。

[
    {
      "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": "/users/2/image",
          "username": "faisal",
          "id": 2
        },
        {
          "image": "/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": "/users/3/image",
          "username": "zaid",
          "id": 3
        },
        {
          "username": "admin@dutchit.com",
          "id": 1
        },
        {
          "image": "/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"
    }
  ];

数组中的每个对象都是一个附加了一些用户的事件。我的角度应用程序中有两个按钮用于显示所有事件,另一个用于显示事件的用户属性与当前用户匹配的事件。因此,例如当前用户具有id 2.然后当他点击我的事件按钮时,仅列出2个事件。

我正在使用与ng-repeat相关联的angualr js过滤器,让我在这里分享。

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;


    };
});

Ng-repeat

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

1 个答案:

答案 0 :(得分: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