How to update an array element in AngularJS ?
Last Updated :
01 Aug, 2022
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:
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