缩短的阵列或集合与字段的数组 [英] Shorten an array or collection with an array of fields

查看:121
本文介绍了缩短的阵列或集合与字段的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景


  • 您有一个数组,

  • 您有一个数组的部分的标识(例如:[1,3]),

  • 您想获得一个缩短的数组。

我找不到如果不是在漫长的路该怎么做:

  _。滤波器(...功能(ID){返回ID == 1 || ID == 3})

:是否如 someMethod1 在underscore.js存在

  VAR frdsArr =
[
 {ID:1,名称:'萌',年龄:40},
 {ID:2,名称:'拉里',年龄:50},
 {ID:3,名称:'花',年龄:60}
];
变种goodFrdsArr = _.someMethod1(frdsArr,'ID',[1,3]);
/ *
goodFrdsArr为[
 {ID:1,名称:'萌',年龄:40},
 {ID:3,名称:'花',年龄:60}
];
* /


与Backbone.js的同样的问题:关于 someMethod2

什么

  VAR好友= Backbone.Model.extend({});
VAR FriendsCollection = Backbone.Collection.extend({
  型号:朋友
});VAR friendsCollection =新FriendsCollection(frdsArr);
VAR goodFriendsCollection = friendsCollection.someMethod2('ID',[1,3]);
/ *
goodFriendsCollection包含朋友模型的两个实例
* /


解决方案

一旦你找到了一个合适的功能,你可以用的 _。MIXIN 简化以后调用。例如,您可以创建 _限制来匹配您的 _ someMethod1 签名:

  _。混入({
    //筛选的具有匹配值的列表中的属性列表
    // _.indexOf接受一个可选的参数isSorted所以让我们把它传递
    限制:功能(列表,ATTR,价值观,isSorted){        返回_.filter(列表功能(OBJ){
            返回_.indexOf(值,OBJ [ATTR],isSorted)== -1!;
        });    }
});变种goodFrdsArr = _.restrict(frdsArr,'ID',[1,3]);

请参阅 http://jsfiddle.net/nikoshr/cf0qs5gh/ 以演示

通过此设置,您可以修改 Backbone.Collection 的原型给您所有的收藏这个新的产能(或多或少从骨干源$ C ​​$ C拍摄):

  Backbone.Collection.prototype.restrict =功能(){
    变参= [] .slice.call(参数);
    args.unshift(this.models);
    返回_.restrict.apply(_,参数);
};VAR friendsCollection =新Backbone.Collection(frdsArr);
变种好伙伴= c.restrict('编号',[1,3]); //使用2种型号的数组

http://jsfiddle.net/nikoshr/cf0qs5gh/1/

Scenario:

  • you have an array,
  • you have an array of some ids (eg: [1,3]),
  • you want to get a shortened array.

I cannot find how to do so if not in the lengthy way:

_.filter(... function (id) { return id==1 || id==3 })

Question: does such someMethod1 exist in underscore.js?

var frdsArr = 
[
 {id: 1, name: 'moe', age: 40}, 
 {id: 2, name: 'larry', age: 50}, 
 {id: 3, name: 'curly', age: 60}
];


var goodFrdsArr = _.someMethod1( frdsArr, 'id', [1, 3] );
/*
goodFrdsArr is [
 {id: 1, name: 'moe', age: 40}, 
 {id: 3, name: 'curly', age: 60}
];
*/


The same question with backbone.js: what about someMethod2?

var Friend = Backbone.Model.extend({});
var FriendsCollection = Backbone.Collection.extend({
  model: Friend
});

var friendsCollection = new FriendsCollection( frdsArr );
var goodFriendsCollection = friendsCollection.someMethod2( 'id', [1,3] );
/*
goodFriendsCollection contains 2 instances of Friend Models
*/

解决方案

Once you have found a suitable function, you can extend Underscore with _.mixin to simplify later calls. For example, you could create _.restrict to match your _.someMethod1 signature:

_.mixin({
    // filter a list on an attribute having to match a list of values
    // _.indexOf accepts an optional isSorted argument so let's pass it
    restrict: function(list, attr, values, isSorted) {

        return _.filter(list, function(obj) {
            return _.indexOf(values, obj[attr], isSorted) !== -1;
        });

    }
});

var goodFrdsArr = _.restrict(frdsArr, 'id', [1, 3]);

See http://jsfiddle.net/nikoshr/cf0qs5gh/ for a demo

With this setup, you can modify Backbone.Collection's prototype to give all your collections this new capacity (more or less taken from the Backbone source code):

Backbone.Collection.prototype.restrict = function() {
    var args = [].slice.call(arguments);
    args.unshift(this.models);
    return _.restrict.apply(_, args);
};

var friendsCollection = new Backbone.Collection(frdsArr);
var goodfriends = c.restrict('id', [1, 3]);  //an array with 2 models

http://jsfiddle.net/nikoshr/cf0qs5gh/1/

这篇关于缩短的阵列或集合与字段的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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