Jai Kumar Rajput
Jai Kumar Rajput

Reputation: 4217

Hash operations in array of the hashes - Angularjs

I have an array with having following elements:

$scope.users = [{"id": "1", "name": "Jai Rajput"}, {"id":"2", "name": "Nakul Sharma"}, {"id": "3", "Name": "Lovey Rajput"}]

Now I want to perform an update and delete in the shortest way on this Array.

Upvotes: 0

Views: 88

Answers (1)

kind user
kind user

Reputation: 41893

Both operations can be made with Array#find function.

let $scope = {};
$scope.users = [{"id": "1", "name": "Jai Rajput"}, {"id":"2", "name": "Nakul Sharma"}, {"id": "3", "Name": "Lovey Rajput"}];

$scope.users.find(v => v.id == 1).name = "Jai Kumar Rajput";
$scope.users.splice($scope.users.indexOf($scope.users.find(v => v.id == 3)), 1);

console.log($scope.users);

Upvotes: 1

Related Questions