Backbone.JS Where()

The Backbone.JS Where() collection method is used to display the model by using the matched attribute in the collection.

Syntax:

Collection.Where ( attribute )   

Parameters:
attribute: This parameter is used to specify the attribute of a model in a collection.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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: 10},
{name: "Joy", id: 30},
{name: "Smiley", id: 20}
]);
var X = names.where({name: "Joy"});
document.write("Total matched name attribute:", + X.length);
</script>Total matched name attribute:2
<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: 10}, {name: "Joy", id: 30}, {name: "Smiley", id: 20} ]); var X = names.where({name: "Joy"}); document.write("Total matched name attribute:", + X.length); </script>Total matched name attribute:2
  

  
Example  
  
  
  
  
  
Total matched name attribute:2  
  
  

Output:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Total matched name attribute:2
Total matched name attribute:2
Total matched name attribute:2

Explanation:
In the above example, the Where() method returns the number of matched value for the “name” attributes of the models in a collection.