HomeLanguagesJavascriptBackbone.js findWhere Collection

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.

  • 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 check with the attribute name:ram.

HTML




<!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.

HTML




<!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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32688 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7057 POSTS0 COMMENTS
Nicole Veronica
12198 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12293 POSTS0 COMMENTS
Shaida Kate Naidoo
7182 POSTS0 COMMENTS
Ted Musemwa
7447 POSTS0 COMMENTS
Thapelo Manthata
7160 POSTS0 COMMENTS
Umr Jansen
7150 POSTS0 COMMENTS