Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Underscore equivalent of _.pick for arrays

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");
});
like image 987
Mohammad Sepahvand Avatar asked Mar 09 '14 09:03

Mohammad Sepahvand


People also ask

What is _ pick?

The _. pick() method is used to return a copy of the object that is composed of the picked object properties. Syntax: _.pick( object, paths )

What is the use of IS underscore array function?

The Underscore. js is a JavaScript library that provides a lot of useful functions like the map, filter, invoke etc even without using any built-in objects. The _. isArray() function is used to find whether the passed argument is an array or not.

What is the use of underscore in JavaScript?

Underscore ( _ ) is just a plain valid character for variable/function name, it does not bring any additional feature. However, it is a good convention to use underscore to mark variable/function as private. You can check Underscore prefix for property and method names in JavaScript for some previous discussion.

What are underscore functions?

Underscore. JS is a popular javascript based library which provides 100+ functions to facilitate web development. It provides helper functions like map, filter, invoke as well as function binding, javascript templating, deep equality checks, creating indexes and so on.


1 Answers

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();
like image 56
thefourtheye Avatar answered Oct 07 '22 00:10

thefourtheye