How to update an array element in AngularJS ? Last Updated : 01 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given an array with an initial array of elements & the task is to update an array element using AngularJS. To update a particular item in an array, there are 2 ways, i.e., either by its value or by its index. Here, we will use the concept of the Property accessors method that is used to access the properties of an object using the bracket notation. Approach: In the first example, the element is updated by its value. Here, we need to check the index of the first occurrence of the search element, provided as a parameter to the function. Then, assign the new value to it. Example 1: This example describes updating an array element by its value. Here, we have used the Array indexOf() Method to find the index of the first occurrence of the search element. HTML <!DOCTYPE html> <html> <head> <script src= "//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"> </script> <script> var myApp = angular.module('app', []); myApp.controller('controller', function ($scope) { $scope.array = ['Geeks', 'Geek', 'gfg', 'GFG']; $scope.updateEl = function (item) { var index = $scope.array.indexOf(item); if (index > -1) { $scope.array[index] = 'GeeksforGeeks'; } }; }); </script> </head> <body style="text-align: center"> <h1 style="color: green"> GeeksforGeeks </h1> <h3> How to update an array element in AngularJS </h3> <div ng-app="app"> <div ng-controller="controller"> <p>Update element in array = {{array}}</p> <input type="button" value="Update element 'gfg' " ng-click="updateEl('gfg')" /> </div> </div> </body> </html> Output: Approach: In the second example, the element is updated using the index. Here, we need to check the index of the search element that is provided as an argument to the function, then assign the new value using its index. Example 2: This example describes updating the array element using an index. HTML <!DOCTYPE HTML> <html> <head> <script src= "//ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.min.js"> </script> <script> var myApp = angular.module("app", []); myApp.controller("controller", function ($scope) { $scope.array = ['Geeks', 'Geek', 'gfg', 'GFG']; $scope.updateEl = function (index) { if (index > -1) { $scope.array[index] = 'GeeksforGeeks'; } }; }); </script> </head> <body style="text-align:center;"> <h1 style="color:green;"> GeeksforGeeks </h1> <h3> How to update an array element in AngularJS </h3> <div ng-app="app"> <div ng-controller="controller"> <p>Update element in array = {{array}}</p> <input type="button" value="Update element at index 3 " ng-click="updateEl(3)"> </div> </div> </body> </html> Output: Comment More infoAdvertise with us Next Article How to update an array element in AngularJS ? P PranchalKatiyar Follow Improve Article Tags : Web Technologies AngularJS AngularJS-Questions Similar Reads How to push elements in an array using AngularJS ? Given an array and the task is to perform the push operation on the array using AngularJS. The arr.push() method is used to push one or more values into the array, & new values will add at the end of an array. This method changes the length of the array by the number of elements added to the arr 2 min read How to make empty an array using AngularJS ? Given an array & the task is to make empty an array or delete all the elements from the array in AngularJS. In order to do this, there are 2 ways i.e., either use the [] notation to reinitialize the array which eventually removes all the elements from the array, or set the length of the array to 2 min read How to count array items in AngularJS ? Given an array and the task is to get the length of an array variable in AngularJS. For this, we will be using the .length() method to get the length of an array variable. Syntax: array.length();Example 1: In this example, the array length is shown by the alert box. Here, we have an array containing 2 min read How to Filter an Array based on user input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. Angular filters can be added in AngularJS to format data. 3 min read How to print an array in table format using angularJS? Given an array & the task is to print the given array in the tabular format using AngularJS. In JavaScript, data can be stored in the form of arrays. Each of the array items has unique indexing, starting from 0. But what if the developer wants to display all the items that are in the array, on t 4 min read How to Sort an Array based on User Input in AngularJS ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. In this article, we will see how to sort an array based o 3 min read How to remove an item from an array in AngularJS Scope? In the AngularJS framework, we can manipulate the data that is within the scope, for example, we can perform the operations on an array by removing its elements. This helps us for better efficiency in developing the web application.We can remove the array elements in the applications like to-do list 5 min read How to set, get and clear cookies in AngularJS ? In this article, we will see how to set, get & clear the Cookies in AngularJS, along with understanding the basic usage through the implementation. In AngularJS, we need to use angular-cookies.js to set, get and clear the cookies. You can use the live CDN link for this: https://cdnjs.cloudflare. 2 min read How to set id attribute of an element dynamically using AngularJS ? In this article, we will see how to change the ID of any element dynamically using AngularJS, along with understanding its implementation through examples.Approach 1: In this approach, a function is called when the button is clicked, which changes the ID of the element to id-5. We are calling a func 3 min read How to bind select element to an object in Angular ? AngularJS is a JavaScript-based framework. It can be used by adding it to an HTML page using a <script> tag. AngularJS helps in extending the HTML attributes with the help of directives and binding of data to the HTML with expressions. An Angular service is a broad category that consists of an 4 min read Like