Ember.js EmberArray indexOf() Method
Last Updated :
23 Dec, 2022
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 indexOf() method is used to find the index of a given object in the array.
Syntax:
indexOf( obj, startAt );
Parameters:
- obj: It is the item whose index we have to find.
- startAt: The index from where to start the search.
Returns: It returns the index or -1 if object is not present.
Installation Steps:
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 Index1
app/routes/Index1.js
import Route from '@ember/routing/route' ;
export default class ListRoute extends Route {
list = [ "apple" , "grapes" , "Banana" ,
"Tomato" , "Onion" , "Potato" , "Garlic" ,
"Dosa" , "Idali" , "Samosa" , "Orange" ];
temp;
idx;
model() {
return this .list;
}
setupController(controller, model) {
super .setupController(controller, model);
controller.set( 'list' , this .list);
controller.set( 'temp' , this .temp);
controller.set( 'idx' , this .idx);
}
}
|
app/controllers/Index1.js
import Ember from 'ember' ;
import {
clear, insertAt, indexOf, lastIndexOf,
includes
} from '@ember/array' ;
export default Ember.Controller.extend({
actions: {
selectattr(temp) {
this .set( 'temp' , temp);
},
checkIndex(data) {
let res = this .list.indexOf(data);
alert((res != -1) ? `Index of ${data} is
${res}` : "List not contains item" )
},
},
});
|
app/templates/Index1.hbs
{{page-title "List" }}
<h3>Here is a list of Items: </h3>
<ul>
{{ #each @model as |i|}}
<li>{{i}}</li>
{{/each}}
</ul>
<br /><br />
<br />
<div>
<label>Select Attribute: </label>
<select onchange={{action "selectattr"
value= "target.value" }}>
{{ #each this.list as |attribute|}}
<option value={{attribute}}>{{attribute}}</option>
{{/each}}
</select>
</div>
<br />
<div>
<input
type= "button"
id= "get-Index"
value= "Check Index"
{{action "checkIndex" this .temp}}
/>
</div>
{{outlet}}
|
Output: Visit localhost:4200/Index1 to view the output

Ember.js EmberArray indexOf method
Example 2: Type the following code to generate the route for this example:
ember generate route Index2
app/routes/Index2.js
import Route from '@ember/routing/route' ;
export default class PartyRoute extends Route {
student = [
'Rahul' ,
'Sam' ,
'Chitransh' ,
'Pankaj' ,
'Yogesh' ,
'Balit' ,
'Pokhraj' ,
'Gulshan' ,
'Sameer' ,
'Bhavesh' ,
'Rohan' ,
'Ranju' ,
'Manisha' ,
'Alok' ,
'Chitransh'
];
item;
idx;
model() {
return this .student;
}
setupController(controller, model) {
super .setupController(controller, model);
controller.set( 'student' , this .student);
controller.set( 'item' , this .item);
controller.set( 'idx' , this .idx);
}
}
|
app/controllers/Index2.js
import Ember from 'ember' ;
import {
insertAt, indexOf, lastIndexOf,
includes
} from '@ember/array' ;
export default Ember.Controller.extend({
actions: {
insertItem(item, idx) {
this .student.insertAt(idx - 1, item);
},
getRank(item) {
let res = this .student.indexOf(item);
alert(`Rank of ${item} : ${res + 1} `);
},
},
});
|
app/templates/Index2.hbs
{{page-title "Rank Of Student" }}
<h3>Rank of Students: </h3>
<ul>
{{ #each @model as |party|}}
<li>{{party}}</li>
{{/each}}
</ul>
<br /><br />
<div>
<label>Enter Item: </label>
{{input value= this .item}}
</div>
<div>
<label>Enter Index: </label>
{{input value= this .idx}}
</div>
<div>
<input
type= "button"
id= "insert-item"
value= "Insert Item"
{{action "insertItem" this .item this .idx}}
/>
</div>
<br /><br />
<div>
<label>Enter Item: </label>
{{input value= this .item}}
</div>
<div>
<input
type= "button"
id= "first-index-item"
value= "Show Rank"
{{action "getRank" this .item}}
/>
</div>
{{outlet}}
|
Output: Visit localhost:4200/Index1 to view the output

Ember.js EmberArray indexOf Method
Reference: https://api.emberjs.com/ember/4.4/classes/EmberArray/methods/indexOf?anchor=indexOf