Open In App

Ember.js Ember.NativeArray isEvery() Method

Last Updated : 13 Jan, 2023
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 isEvery() method is used to check the value against the specific key is the same for all the objects or not.

Syntax:

isEvery( key, value );

Parameters:

  • key: It is the name of the property that we want to check.
  • value: It is the value to test against the key.

Return Value: Boolean.

Steps to Install and Run Ember.js:

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 isEvery1
  • app/routes/isEvery1.js

Javascript




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/isEvery1.js

Javascript




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/isEvery1.hbs

Javascript




{{page-title "isEvery"}}
<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/isEvery1 to view the output

Ember.js Ember.NativeArray isEvery method

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

ember generate route isEvery2
  • app/routes/isEvery2.js

Javascript




import Route from '@ember/routing/route';
import { } from '@ember/array';
 
export default class FruitsRoute extends Route {
    item1 = [
        {
            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',
        }
    ];
    item2 = [
        {
            name: 'Lady Finger',
            isFruit: false,
            color: 'green',
        },
        {
            name: 'Brinjal',
            isFruit: false,
            color: 'purple',
        },
        {
            name: 'Potato',
            isFruit: false,
            color: 'brown',
        },
        {
            name: 'Onion',
            isFruit: false,
            color: 'violet',
        },
    ];
    model() {
        return this.item1;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('item1', this.item1);
        controller.set('item2', this.item2);
    }
}


  • app/controllers/isEvery2.js

Javascript




import Ember from 'ember';
import { isAny, isEvery, addObjects, findBy }
    from '@ember/array';
 
export default Ember.Controller.extend({
    actions: {
        Find_item(data) {
            let foo = this.item1.findBy('name', data);
            alert(foo ? `${foo.name} present in list`
                : 'No items not present in list');
        },
        AddItems() {
            this.item1.addObjects(this.item2);
        },
        anyFruit() {
 
            let ans = this.item1.isAny("isFruit")
            alert(ans ? `List contains SomeFruit`
                : "List doesn't contains any Fruits");
        },
        allFruit() {
            let ans = this.item1.isEvery("isFruit");
            alert(ans ? `List contains Only Fruits`
                : "List doesn't contains Only Fruits");
        },
 
    },
});


  • app/templates/isEvery2.hbs

Javascript




{{page-title "isEvery"}}
 
<h2>List of Item </h2>
 
<table >
    <tr>
        <th>Name</th>
        <th>Fruit</th>
        <th>Color</th>
    </tr>
    {{#each @model as |Eatable|}}
    <tr>
        <td>{{Eatable.name}}</td>
        <td>{{Eatable.isFruit}}</td>
        <td>{{Eatable.color}}</td>
    </tr>
    {{/each}}
</table>
<br/>
<div>
    <label>Enter value: </label>
    {{input value=this.temp}}
</div>
<input type="button"
    id="greet"
    value="Find Item"
    {{action 'Find_item' this.temp}} />
 
<br/><br/>
 
<br /><br />
<input type="button" id="add-items"
    value="Add Items"
    {{action 'AddItems' }} />
 
<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'}}/>
 
{{outlet}}


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

Ember.js Ember.NativeArray isEvery method

Reference: https://api.emberjs.com/ember/4.4/classes/Ember.NativeArray/methods/isEvery?anchor=isEvery



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads