In this article, we will discuss Backbone.js where collection. The Backbone.js is where the collection is used to retrieve and return the model with the specified matched attribute in the given model array of collection.
Syntax:
Backbone.Collection.where(attribute)
Parameters: It will take only one parameter.
- attribute: attribute is used to take an element from the model collection and return if it is found in the collection
Example 1: In this example, we will create a model array of collections and apply where collection with attributes ‘Ram’ and ‘Shivam’ by returning a number of times repeated.
HTML
<!DOCTYPE html> < html > < head > < title >Example of Backbone.js</ title > type = "text/javascript" ></ script > < script src = type = "text/javascript" ></ script > < script src = type = "text/javascript" ></ script > < script type = "text/javascript" > var datas = Backbone.Model.extend({ defaults: { name: "sravan" } }); var datacollection = Backbone.Collection.extend({ model: datas }); var data1 = new datas({ name: "ram" }); var data2 = new datas({ name: "shivam" }); var data3 = new datas({ name: "ram" }); var data4 = new datas({ name: "ram" }); var final = new datacollection(); final.add([data1, data2, data3, data4]); document.write("Actual Values:", JSON.stringify(final.toJSON())); document.write("< br >"); // Find for ram var result1 = final.where({ name: "ram" }); document.write("Total ram found: ", +result1.length); document.write("< br >"); // Find for shivam var result1 = final.where({ name: "shivam" }); document.write("Total shivam found: ", +result1.length); document.write("< br >"); </ script > </ head > < body ></ body > </ html > |
Output:
Actual Values:[ {"name":"ram"},{"name":"shivam"}, {"name":"ram"},{"name":"ram"} ] Total ram found: 3 Total shivam found: 1
Example 2: In this example, we will create a model array of collections that has 2 attributes and apply where collection with attributes ‘ram’ and ‘Shivam’. by returning a number of times repeated.
HTML
<!DOCTYPE html> < html > < head > < title >Example of Backbone.js</ title > type = "text/javascript" ></ script > < script src = type = "text/javascript" ></ script > < script src = type = "text/javascript" ></ script > < script type = "text/javascript" > var datas = Backbone.Model.extend({ defaults: { name: "sravan" } }); var datacollection = Backbone.Collection.extend({ model: datas }); var data1 = new datas({ name: "ram", id: 4 }); var data2 = new datas({ name: "shivam", id: 3 }); var data3 = new datas({ name: "ram", id: 5 }); var data4 = new datas({ name: "ram", id: 8 }); var final = new datacollection(); final.add([data1, data2, data3, data4]); document.write("Actual Values:", JSON.stringify(final.toJSON())); document.write("< br >"); // Find for ram var result1 = final.where({ name: "ram", id: 4 }); document.write("Total ram found: ", +result1.length); document.write("< br >"); // Find for shivam var result1 = final.where({ name: "shivam", id: 43 }); document.write("Total shivam found: ", +result1.length); document.write("< br >"); </ script > </ head > < body ></ body > </ html > |
Output:
Actual Values:[ {"name":"ram","id":4},{"name":"shivam","id":3}, {"name":"ram","id":5},{"name":"ram","id":8} ] Total ram found: 1 Total shivam found: 0
Reference: https://backbonejs.org/#Collection-where