Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Underscore.js _.pluck in jQuery

Tags:

Does anyone know of a 'pluck' plugin that matches the underscore array method?

pluck_.pluck(list, propertyName)  

A convenient version of what is perhaps the most common use-case for map: extracting a list of property values.

var stooges = [{name : 'moe', age : 40}, {name : 'larry', age : 50}, {name : 'curly', age : 60}]; _.pluck(stooges, 'name'); => ["moe", "larry", "curly"] 

Google is not helping me much today. Any pointers much appreciated

like image 503
Chin Avatar asked Feb 17 '12 13:02

Chin


People also ask

What is jQuery pluck?

console.log(_.pluck(users, 'id')); </ script > </ body > </ html > jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it's philosophy of “Write less, do more”.

What is _ each in JavaScript?

The _each method does exactly what it sounds like. It works on collections (arrays or objects), and will iterate over each element in the collection invoking the function you specified with 3 arguments (value, index, list) with index being replaced by key if used on an object.

Is underscore js still used?

Lodash and Underscore are great modern JavaScript utility libraries, and they are widely used by Front-end developers.

What is _ template?

The _. template() function is an inbuilt function in the Underscore. js library of JavaScript which is used to compile JavaScript templates into functions that can be evaluated for rendering. Useful for rendering complicated bits of HTML from JSON data sources.


1 Answers

You can do it with an expression;

var arr = $.map(stooges, function(o) { return o["name"]; }) 
like image 54
Alex K. Avatar answered Oct 19 '22 09:10

Alex K.