The Backbone.JS Pluck() collection method retrieves the attributes from a model in a collection.
Syntax:
Collection.Pluck ( attribute )
Parameters:
attribute: This parameter is used to specify the attribute of a model in a collection.
Example:
<title>Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
<script type="text/javascript">
var names = new Backbone.Collection([
{name: "Joy", id: 1},
{name: "Happy", id: 2},
{name: "Smiley", id: 3}
]);
document.write("Attributes returned on pluck: <br/>",names.pluck('name'));
</script>Attributes returned on pluck: <br>Joy,Happy,Smiley
<title>Example</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js" type="text/javascript"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.1.2/backbone-min.js" type="text/javascript"></script>
<script type="text/javascript">
var names = new Backbone.Collection([
{name: "Joy", id: 1},
{name: "Happy", id: 2},
{name: "Smiley", id: 3}
]);
document.write("Attributes returned on pluck: <br/>",names.pluck('name'));
</script>Attributes returned on pluck: <br>Joy,Happy,Smiley
Example Attributes returned on pluck:
Joy,Happy,Smiley
Output:
Attributes returned on pluck:
Joy,Happy,Smiley
Attributes returned on pluck:
Joy,Happy,Smiley
Attributes returned on pluck: Joy,Happy,Smiley
Explanation:
In the above example, the Pluck() method returns the “name” attributes from the model instances of a collection.