Open In App

Ember.js EmberArray lastIndexOf() Method

Last Updated : 22 Sep, 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 lastIndexOf() method is used to get the index of the given object’s last occurrence.

Syntax:

lastIndexOf( object, startAt );

 

Parameters:

  • object: It is the item that we want to search for.
  • startAt: It is the starting location to search.

Return Value:  Index or -1 if not found.

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 lastIndexOf1

app/routes/lastIndexOf1.js




import Route from '@ember/routing/route';
  
export default class ListRoute extends Route {
    list = ['a', 'z', 'k', 'l', 'c', 'm', 'l'];
    list2 = ['a', 'o', 'm', 'b', 'z', 'a'];
    item;
    idx;
    model() {
        return this.list;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('list', this.list);
        controller.set('list2', this.list2);
        controller.set('item', this.item);
        controller.set('idx', this.idx);
    }
}


app/controllers/lastIndexOf1.js




import Ember from 'ember';
import { clear, insertAt, indexOf, lastIndexOf,
    includes } from '@ember/array';
  
export default Ember.Controller.extend({
    actions: {
        insertItem() {
            this.list.pushObjects(this.list2);
        },
        getLastIndex(item) {
            let res = this.list.lastIndexOf(item);
            alert(res);
        },
        getFirstIndex(item) {
            let res = this.list.indexOf(item);
            alert(res);
        },
    },
});


app/templates/lastIndexOf1.hbs




{{page-title "LastIndexOf"}}
<h3>Here is a list of Items: </h3>
<ul>
    {{#each @model as |i|}}
    <li>{{i}}</li>
    {{/each}}
</ul>
<br /><br />
  
<div>
    <label>Enter Item: </label>
    {{input value=this.item}}
</div>
<div>
    <input
        type="button"
        id="first-index-item"
        value="Get First Index"
        {{action "getFirstIndex" this.item}}
/>
</div>
<br/>
<div>
    <input
        type="button"
        id="last-index-item"
        value="Get Last Index"
        {{action "getLastIndex" this.item}}
    />
</div>
<br/><br/>
<div>
    <input
        type="button"
        id="insert-item"
        value="Insert Item"
        {{action "insertItem" }}
    />
</div>
{{outlet}}


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

Ember.js EmberArray lastIndexOf method

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

ember generate route lastIndexOf2

app/routes/lastIndexOf2.js




import Route from '@ember/routing/route';
import { } from '@ember/array';
  
export default class FruitsRoute extends Route {
    item1 = ['Apple', 'Grapes', 'Mango', 'Watermelon',
        'Orange', 'Lady Finger', 'Brinjal', 'Potato', 'Onion',
    ];
    item;
    item2;
    model() {
        return this.item1;
    }
    setupController(controller, model) {
        super.setupController(controller, model);
        controller.set('item1', this.item1);
        controller.set('item', this.item);
        controller.set('item2', this.item2)
    }
}


app/controllers/lastIndexOf2.js




import Ember from 'ember';
import { pushObjects, isAny } from '@ember/array';
  
export default Ember.Controller.extend({
    actions: {
        Check_item(data) {
            let ans = this.item1.lastIndexOf(data);
            alert(ans);
        },
        Check_item2(data) {
            let ans = this.item1.lastIndexOf(data);
            ans != -1
                ? alert(`Yes it contains Item`)
                : alert(`It doesn't contains Item`);
        },
    },
});


app/templates/lastIndexOf2.hbs




{{page-title "Fruits"}}
<table style=" border-spacing : 30px">
<h3>Here is a Bucket: </h3>
<ul>
    {{#each @model as |eatable|}}
    <li>{{eatable}}</li>
    {{/each}}
</ul>
</table>
<br /><br />
<div>
    <label>Enter Item: </label>
    {{input value=this.item2}}
</div>
  
<input type="button"
    id="fruit-all"
    value="Search item"
    {{action 'Check_item' this.item2 }} />
<br /><br />
  
<div>
    <label>Enter Item: </label>
    {{input value=this.item}}
</div>
<input type="button"
    id="fruit-notAll"
    value="Contains"
    {{action 'Check_item2' this.item}} />
  
{{outlet}}


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

Ember.js EmberArray lastIndexOf method

Reference: https://api.emberjs.com/ember/2.14/classes/Ember.Array/methods/lastIndexOf?anchor=lastIndexOf



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

Similar Reads