Backbone.js的Underscore.js过滤收集VS阵列 [英] Backbone.js Underscore.js filtering a collection vs array

查看:159
本文介绍了Backbone.js的Underscore.js过滤收集VS阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上试图做什么用这一问题完成的,但具有一个数组并返回使用相应的数组,而不仅仅是该值的值的所有的对象:
<一href=\"http://stackoverflow.com/questions/11762105/filter-backbone-collection-by-attribute-value\">Filter骨干集​​合根据属性值

我的天性是使用_.filter或_.partition的人收藏,但我对如何这些被比较和放困惑;回来了,我没有得到期望的结果。

通过以​​下,通过路由器传递ID中:

  friendsRoute:功能(ID){
  人=新App.Collections.Persons();
  persons.fetch()。完成(功能(){
    VAR单= persons.find(功能(我){
      返回i.get('身份证')== ID;
    });
    VAR friendsIds = single.get('朋友');
    VAR的朋友=?
    // VAR friendsList =新App.Views.FriendsList();
    //friendsList.render({朋友:朋友});
  });
},

我有一个friendsIds阵列(Joe的朋友):

  friendsIds = [1,4,5]

和试图从以下JSON获得匹配的ID和我friendsList视图中显示它们的名称:

  [
  {ID:1,名:史蒂夫,年龄:22,朋友:[4]},
  {ID:2,名:玛丽,时代:18,朋友:[1,3]},
  {ID:3,名:乔,时代:43,朋友:[1,4,5]},
  {ID:4,名:汤米,时代:19,朋友:[1]},
  {ID:5,名:张国荣,年龄:27,朋友:[2,4]}
]


解决方案

我想你可以使用 <$ C的组合$ C>地图 findWhere 做到这一点:

  VAR的朋友= _.map(friendsIds,功能(ID){
    返回_.findWhere(人,{ID:ID});
});

I'm basically trying to do what is done with this question, but with an array and return all objects using the values of the corresponding array, not just the value: Filter backbone collection by attribute value

My natural instinct is to use _.filter or _.partition on the persons collection, but am confused on how these are being compared & returned and am not getting the desired outcome.

With the following, passing the id in via the router:

friendsRoute: function(id){                                                                       
  persons = new App.Collections.Persons();                                                   
  persons.fetch().done(function(){                                                           
    var single = persons.find(function(i){                                                   
      return i.get('id') == id;                                                              
    });                                                                                      
    var friendsIds = single.get('friends');


    var friends = ?


    //var friendsList = new App.Views.FriendsList();
    //friendsList.render({ friends: friends });
  });                                                                                        
},                                                                                           

I have a friendsIds array (Joe's friends):

friendsIds = [1,4,5]

And trying to get the matched ids from the following JSON and display their names in my friendsList view:

[
  { "id": 1, "name": "Steve", "age": 22, "friends": [4] },
  { "id": 2, "name": "Mary", "age": 18, "friends": [1,3] },
  { "id": 3, "name": "Joe", "age": 43, "friends": [1,4,5] },
  { "id": 4, "name": "Tommy", "age": 19, "friends": [1] },
  { "id": 5, "name": "Leslie", "age": 27, "friends": [2,4] }
]

解决方案

I think you could use a combination of map and findWhere to do this:

var friends = _.map(friendsIds, function(id) { 
    return _.findWhere(persons, { 'id': id }); 
});

这篇关于Backbone.js的Underscore.js过滤收集VS阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆