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
Similar Reads
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
JavaScript Tutorial
JavaScript is a programming language used to create dynamic content for websites. It is a lightweight, cross-platform, and single-threaded programming language. JavaScript is an interpreted language that executes code line by line providing more flexibility. HTML adds Structure to a web page, CSS st
11 min read
Web Development
Web development is the process of creating, building, and maintaining websites and web applications. It involves everything from web design to programming and database management. Web development is generally divided into three core areas: Frontend Development, Backend Development, and Full Stack De
5 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
React Interview Questions and Answers
React is an efficient, flexible, and open-source JavaScript library that allows developers to create simple, fast, and scalable web applications. Jordan Walke, a software engineer who was working for Facebook created React. Developers with a Javascript background can easily develop web applications
15+ min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
JavaScript Interview Questions and Answers
JavaScript (JS) is the most popular lightweight, scripting, and interpreted programming language. JavaScript is well-known as a scripting language for web pages, mobile apps, web servers, and many other platforms. It is essential for both front-end and back-end developers to have a strong command of
15+ min read
HTML Tutorial
HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
10 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
Backpropagation in Neural Network
Backpropagation is also known as "Backward Propagation of Errors" and it is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network. In this article we will explore what
10 min read