Open In App

Backbone.js findWhere Collection

In this article, we will discuss the Backbone.js findWhere collection. The Backbone.js findWhere collection is used to retrieve and return the first value with the specified matched attribute in the given model array of collection.

Syntax:



Backbone.Collection.findWhere(attribute)    

Parameter: It takes a single parameter.

Example 1: In this example, we will check with the attribute name:ram.




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <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>");
  
        // Using the findWhere() method
        var result1 = final.findWhere({ name: "ram" });
        document.write(
          "Matched Attribute: ",
          JSON.stringify(result1)
        );
    </script>
</body>
 
</html>

Output:



Actual Values:[{"name":"ram"},{"name":"shivam"},
    {"name":"ram"},{"name":"ram"}]
Matched Attribute: {"name":"ram"}

Example 2: In this example, we will check with attribute name:shivam.




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
    <script type="text/javascript" src=
    </script>
</head>
 
<body>
    <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>");
 
        // Using the findWhere() method
        var result1 = final.findWhere(
          { name: "shivam" }
        );
        document.write(
          "Matched Attribute: ",
          JSON.stringify(result1)
        );
    </script>
</body>
 
</html>

Output:

Actual Values:[{"name":"ram"},{"name":"shivam"},
    {"name":"ram"},{"name":"ram"}]
Matched Attribute: {"name":"shivam"}

Reference: https://backbonejs.org/#Collection-findWhere


Article Tags :