强调了阵列_.pick等效 [英] Underscore equivalent of _.pick for arrays

查看:121
本文介绍了强调了阵列_.pick等效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,挑来取回如果只指定属性的对象:

  _挑({名称:'萌',年龄:50,用户id:'moe1'},'名','年龄');
= GT; {名称:'萌',年龄:50}

我将如何执行阵列上相同的操作,说我有一个数组,如:

  [{名称:'moe1',年龄:50,用户id:'moe1},
{名称:'moe2',年龄:50,用户id:'moe1},
{名称:'moe3',年龄:50,用户id:'moe1'}]

和我想它映射到一个数组所以只包括名称年龄属性,例如:

  [{名称:'moe1',年龄:50},
{名称:'moe2',年龄:50},
{名称:'moe3',年龄:50}]

我必须做一个每()对数组然后执行挑()各个对象,或者是有一个更清洁的方式?

修改

很抱歉,但只是一个小要求,我将如何执行的地方(即获取所有那些年龄大于50),然后执行
修改
得到它这样做,不知道如何下划线链接的作品。

  _(数据).reject(函数(R){返回d.age的51;})地图(功能(O){
            返回_.pick(邻,年龄,姓名);
});


解决方案

您必须使用 _。图并应用相同的 _。挑上的所有对象。

  VAR数据= [{名称:'moe1',年龄:30,用户id:'moe1},
            {名称:'moe2',年龄:50,用户id:'moe1},
            {名称:'moe3',年龄:60,用户id:'moe1'}]VAR的结果= _.map(数据,功能(currentObject){
    返回_.pick(currentObject,姓名,年龄);
});的console.log(结果);

输出

  [{名称:'moe1',年龄:50},
  {名称:'moe2',年龄:50},
  {名称:'moe3',年龄:50}]

如果你想获得其中年龄> 50的对象,你可能想要做的,像这样

  VAR数据= [{名称:'moe1',年龄:30,用户id:'moe1},
            {名称:'moe2',年龄:50,用户id:'moe1},
            {名称:'moe3',年龄:60,用户id:'moe1'}]功能filterByAge(currentObject){
    返回currentObject.age&放大器;&安培; currentObject.age> 50;
}功能omitUserId(currentObject){
    返回_.omit(currentObject,将把userid);
}VAR的结果= _.map(_过滤器(数据,filterByAge),omitUserId);
的console.log(结果);

输出

  [{名称:'moe3',年龄:60}]

您可以做同样的链接,<一个href=\"http://stackoverflow.com/questions/22280775/underscore-equivalent-of-pick-for-arrays/22280789?noredirect=1#comment33847612_22280789\">as通过rightfold 建议,像这样

  VAR的结果= _.chain(数据).filter(filterByAge).MAP(omitUserId).value的();

I understand that pick is used to get back an object with only specified properties:

_.pick({name: 'moe', age: 50, userid: 'moe1'}, 'name', 'age');
=> {name: 'moe', age: 50}

How would I perform that same operation on an array, say I have an array such as:

[{name: 'moe1', age: 50, userid: 'moe1'},
{name: 'moe2', age: 50, userid: 'moe1'},
{name: 'moe3', age: 50, userid: 'moe1'}]

and I want to map it to an array so to include only the name and age properties, like:

[{name: 'moe1', age: 50},
{name: 'moe2', age: 50},
{name: 'moe3', age: 50}]

Do I have to do an each() on the array then perform a pick() on each object, or is there a cleaner way?

EDIT

Sorry but just another small requirement, how would I perform a where (i.e. get all those whose age is greater than 50) and then perform the pick? EDIT got it done like this, was unaware of how chaining works in underscore.

_(data).reject(function (r) { return d.age<51; }).map(function (o) {
            return _.pick(o, "age", "name");
});

解决方案

You have to use _.map and apply the same _.pick on all the objects.

var data = [{name: 'moe1', age: 30, userid: 'moe1'},
            {name: 'moe2', age: 50, userid: 'moe1'},
            {name: 'moe3', age: 60, userid: 'moe1'}];

var result = _.map(data, function(currentObject) {
    return _.pick(currentObject, "name", "age");
});

console.log(result);

Output

[ { name: 'moe1', age: 50 },
  { name: 'moe2', age: 50 },
  { name: 'moe3', age: 50 } ]

If you want to get the objects in which the age is > 50, you might want to do, like this

var data = [{name: 'moe1', age: 30, userid: 'moe1'},
            {name: 'moe2', age: 50, userid: 'moe1'},
            {name: 'moe3', age: 60, userid: 'moe1'}];

function filterByAge(currentObject) {
    return currentObject.age && currentObject.age > 50;
}

function omitUserId(currentObject) {
    return _.omit(currentObject, "userid");
}

var result = _.map(_.filter(data, filterByAge), omitUserId);    
console.log(result);

Output

[ { name: 'moe3', age: 60 } ]

You can do the same with chaining, as suggested by rightfold, like this

var result = _.chain(data).filter(filterByAge).map(omitUserId).value();

这篇关于强调了阵列_.pick等效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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