Open In App

Ember.js ArrayProxy isAny() Method

Last Updated : 10 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Ember.js is an open-source JavaScript framework used for developing large client-side web applications which is based on Model-View-Controller (MVC) architecture. Ember.js is one of the most widely used front-end application frameworks. It is made to speed up development and increase productivity. Currently, it is utilized by a large number of websites, including Square, Discourse, Groupon, Linked In, Live Nation, Twitch, and Chipotle.

The isAny() method is used to check if the value of the property of any item is present in the array.

Syntax:

isAny( key, value );

 

Parameters:

  • key: It is the name of the property whose value we want.
  • value: It is the value of the property that is tested against the key. 

Return Value: Boolean.

To run the following examples you will need to have an ember project with you. To create one, you will need to install ember-cli first. Write the below code in the terminal:

npm install ember-cli

Now you can create the project by typing in the following piece of code:

ember new <project-name> --lang en

To start the server, type:

ember serve

Example 1: Type the following code to generate the route for this example:

ember generate route isAny1

app/routes/isAny1.js




import Route from "@ember/routing/route";
  
export default class DetailsRoute extends Route {
    details = [
        {
            name: "Anubhav",
            mobile: "1298119967",
            city: "Patna",
            country: "India",
            gender: "M",
            zipCode: "800020",
        },
        {
            name: "Yogesh",
            mobile: "1234567890",
            city: "Raipur",
            country: "India",
            gender: "M",
            zipCode: "402001",
        },
        {
            name: "Satyam",
            mobile: "2222222222",
            city: "Delhi",
            country: "India",
            gender: "M",
            zipCode: "110012",
        },
        {
            name: "Shivam",
            mobile: "1122113322",
            city: "Patna",
            country: "India",
            gender: "M",
            zipCode: "530068",
        },
        {
            name: "Ayushi",
            mobile: "2244668800",
            city: "Jaipur",
            country: "India",
            gender: "F",
            zipCode: "302001",
        },
    ];
  
    city;
    start;
    end;
    model() {
        return this.details;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set("details", this.details);
        controller.set("city", this.city);
        controller.set("start", this.start);
        controller.set("end", this.end);
    }
}


app/controllers/isAny1.js




import Ember from "ember";
import { isAny }
    from "@ember/array";
  
export default Ember.Controller.extend({
    actions: {
        checkCity(city) {
            let res = this.details.isAny('city', city);
            alert(res 
                ? `Person from ${city} Present` 
                : `No Person from ${city} present`);
        },
        checkName(name) {
            let res = this.details.isAny('name', name);
            alert(res 
                ? `${name} is in list` 
                : `No Person of name ${name} present`);
        },
        checkFemale() {
            let res = this.details.isAny('gender', 'F');
            alert(res 
                ? `Female employee present in List` 
                : `No Female present`);
        },
    },
});


app/templates/isAny1.hbs




{ { page - title "isAny" } }
<h3>List of People: </h3>
<br /><br />
<table>
    <tr>
        <th>Name</th>
        <th>Gender</th>
        <th>Mobile</th>
        <th>City</th>
        <th>Country</th>
        <th>Zip Code</th>
    </tr>
    {{#each @model as |detail|}}
    <tr>
        <td>{{detail.name}}</td>
        <td>{{detail.gender}}</td>
        <td>{{detail.mobile}}</td>
        <td>{{detail.city}}</td>
        <td>{{detail.country}}</td>
        <td>{{detail.zipCode}}</td>
    </tr>
    {{/each}}
</table>
<br /><br />
<div>
    <label>Enter City: </label>
    {{input value=this.city}}
</div>
  
<div>
    <input type="button" id="check-city" 
    value="Check SomeOne from city" 
    {{action 'checkCity' this.city}} />
</div>
<br /><br />
<div>
    <label>Enter Name: </label>
    {{input value=this.name}}
</div>
<div>
    <input type="button" id="check-name" 
    value="Check SomeOne by Name" 
    {{action 'checkName' this.name}} />
</div>
<br />
<input type="button" id="get-female" 
    value="Female Present in list" 
    {{action 'checkFemale' }} />


Output: Visit localhost:4200/isAny1 to view the output:

Ember.js ArrayProxy isAny method

Example 2: Type the following code to generate the route for this example:

ember generate route isAny2

app/routes/isAny2.js




import Route from '@ember/routing/route';
import { } from '@ember/array';
  
export default class FruitsRoute extends Route {
    fruits = [
        {
            'name': 'Lady Finger',
            'isFruit': false,
            'color': 'green'
        },
        {
            'name': 'Brinjal',
            'isFruit': false,
            'color': 'purple'
        },
        {
            'name': 'Apple',
            'isFruit': true,
            'color': 'red'
        },
        {
            'name': 'Grapes',
            'isFruit': true,
            'color': 'green'
        },
        {
            'name': 'Mango',
            'isFruit': true,
            'color': 'yellow'
        },
        {
            'name': 'Watermelon',
            'isFruit': true,
            'color': 'red'
        },
        {
            'name': 'Orange',
            'isFruit': true,
            'color': 'orange'
        }
    ];
    model() {
        return this.fruits;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('fruits', this.fruits);
    }
}


app/controllers/isAny2.js




import Ember from "ember";
import { isAny, isEvery } from "@ember/array";
  
export default Ember.Controller.extend({
    actions: {
        anyFruit() {
  
            let ans = this.fruits.isAny("isFruit")
            alert(ans 
                ? `List contains SomeFruit` 
                : "List doesn't contains any Fruits");
        },
        allFruit() {
            let ans = this.fruits.isEvery("isFruit");
            alert(ans 
                ? `List contains Only Fruits` 
                : "List doesn't contains Only Fruits");
        },
        remove() {
            this.fruits.clear();
        }
    },
});


app/templates/isAny2.hbs




{{page-title "isAny"}}
<h3>Here is a list of eatables: </h3>
<ul>
    {{#each @model as |eatable|}}
    <li>
        {{eatable.name}}
    </li>
    {{/each}}
</ul>
<br /><br />
<input type="button" id="fruit-any" 
    value="List Contains Any Fruit?" 
    {{action 'anyFruit' }} />
<br /><br />
<input type="button" id="fruit-all" 
    value="List Contains Only Fruit?" 
    {{action 'allFruit' }} />
  
<br /><br />
<input type="button" id="remove-Fruits" 
    value="Clear" 
    {{action 'remove' }} />
{{outlet}}


Output: Visit localhost:4200/isAny2 to view the output

Ember.js ArrayProxy isAny method

Reference: https://api.emberjs.com/ember/4.6/classes/ArrayProxy/methods/isAny?anchor=isAny



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads