【问题标题】:how to find specific record against array of json objects with complex /nested properties如何针对具有复杂/嵌套属性的json对象数组查找特定记录
【发布时间】:2016-08-17 04:51:54
【问题描述】:

我正在尝试使用 underscorejs 从 ajax 响应中过滤掉稍后将由我的 angualr js 应用程序使用的对象。以下是来自服务器的响应(对象数组)。

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

数组中的每个对象都是一个附加了一些用户的事件。我的 Angular 应用程序中有两个按钮,一个用于显示所有事件,一个用于仅显示事件的用户属性与当前用户匹配的那些事件。因此,例如当前用户的 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-重复

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

【问题讨论】:

    标签: javascript angularjs underscore.js


    【解决方案1】:

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

    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

    【讨论】:

      猜你喜欢
      • 2017-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-18
      相关资源
      最近更新 更多