Open In App

Ember.js EmberArray every() Method

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 every() method is used to check the passed function on every object of the list.

Syntax:

every(callback, target)

 

Parameters:

  • callback: The function to execute on every object.
  • target: The target object to which callback is applied.

Return Value: True if the callback returns true for all objects in the array, else false.

Step 1: 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

Step 2: 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 every1

app/routes/every1.js




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: 'Grapes',
            isFruit: true,
            color: 'green',
        },
        {
            name: 'Mango',
            isFruit: true,
            color: 'yellow',
        },
        {
            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/every1.js




import Ember from 'ember';
import { Every } from '@ember/array';
  
export default Ember.Controller.extend({
    actions: {
        removeFruit() {
            this.fruits.shiftObject();
        },
        Check_item1() {
            this.item1.every((i) => i.isFruit)
                ? alert('List1 only 
                   contains Fruit only')
                : alert('List1 also 
                   contains other items');
        },
        Check_item2() {
            this.item2.every((i) => i.isFruit
                  == false)
                ? alert('Yes it only 
                   contains Vegetables ')
                : alert('No it
                   contains fruits also ');
        },
    },
});


app/templates/every1.hbs




{{page-title "Fruits"}}
<table
    style="float : left ; 
  border-spacing : 30px"
>
    <h3>Here is a list 1: </h3>
    <ul>
        {{#each @model as |eatable|}}
            <li>{{eatable.name}}</li>
        {{/each}}
    </ul>
</table>
<table style="border-spacing : 30px">
    <h3>Here is a list 2: </h3>
    <ul>
        {{#each this.item2 as |eatable|}}
            <li>{{eatable.name}}</li>
        {{/each}}
    </ul>
</table>
<br /><br />
<input
    type="button"
    id="fruit-all"
    value="List 1 Contains Only Fruit?"
    {{action "Check_item1"}}
/>
<br /><br />
<input
    type="button"
    id="fruit-notAll"
    value="List 2 Contains Only Vegetables?"
    {{action "Check_item2"}}
/>
{{outlet}}


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

Ember.js EmberArray every method

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

ember generate route every2

app/routes/every2.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: 'Sakshi',
            mobile: '1234567890',
            city: 'Mumbai',
            country: 'India',
            gender: 'F',
            zipCode: '400001',
        },
        {
            name: 'Satyam',
            mobile: '2222222222',
            city: 'Delhi',
            country: 'India',
            gender: 'M',
            zipCode: '110012',
        },
        {
            name: 'Shivam',
            mobile: '1122113322',
            city: 'Bangalore',
            country: 'India',
            gender: 'M',
            zipCode: '530068',
        },
        {
            name: 'Ayushi',
            mobile: '2244668800',
            city: 'Jaipur',
            country: 'India',
            gender: 'F',
            zipCode: '302001',
        },
    ];
    city;
    code;
    model() {
        return this.details;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('details', this.details);
        controller.set('city', this.city);
        controller.set('code', this.code);
    }
}


app/controllers/every2.js




import Ember from 'ember';
import { every } from '@ember/array';
  
export default Ember.Controller.extend({
    actions: {
        checkCity(code) {
            alert(
                `Contains Person from Outside ${code}: 
                  ${this.details.every((i) =>
                    i.city == code)
                    ? 'No' : 'Yes'
                }`
            );
        },
        checkCountry() {
            alert(
                `All are ${this.details.every((i) =>
                    i.country == 'India')
                    ? '' : 'not'
                } Indian`
            );
        },
        CheckFemale() {
            alert(
                `All are ${this.details.every((i) =>
                    i.gender == 'F')
                    ? '' : 'not'
                } Females`
            );
        },
    },
});


app/templates/every2.hbs




{{page-title "Details"}}
<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.code}}
</div>
<div>
    <input
        type="button"
        id="check-city"
        value="Check Person from outside City"
        {{action "checkCity" this.code}}
    />
</div>
<br /><br />
<div>
    <input
        type="button"
        id="set-code"
        value="All Females"
        {{action "CheckFemale"}}
    />
</div>
<br /><br />
<input
    type="button"
    id="check-country"
    value="All Indian?"
    {{action "checkCountry"}}
/>
{{outlet}}


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

Ember.js EmberArray every method

Reference: https://api.emberjs.com/ember/4.4/classes/EmberArray/methods/every?anchor=every



Last Updated : 23 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads