How to get a particular value from the array in angularjs it can be used in js file itself?

31 Views Asked by At
$scope.login = function () {

    var userLogin = JSON.parse(localStorage.getItem('userData'));

    userLog = (userLogin.Name)
    console.log(userLogin);

}

this is in my browser localstorage

[{" Name":"Admin","EmailId":"[email protected]","password":"Admin","PhoneNo":"9765432108"},{"Name":"testuser2","EmailId":"[email protected]","password":"testing","PhoneNo":9871324560},{"Name":"test11","EmailId":"[email protected]","password":"test","PhoneNo":9632196321}]

From the console i got the output as,

0: Object { " Name": "Admin", EmailId: "[email protected]", password: "Admin", … }

​ but i want to that name and password field in the separate variable

2

There are 2 best solutions below

0
Raaj On BEST ANSWER

$scope.login = function () {

        var userLogin = JSON.parse(localStorage.getItem('userData'));

        for (var i = 0; i < userLogin.length; i++) {

         console.log("EmailId: "userLogin[i].EmailId);
          console.log("password: " userLogin[i].password); 

            }
0
Sagar Pandey On

You can use below code

$scope.login = function () {

var userLogin = JSON.parse(localStorage.getItem('userData'));

for(data of userLogin){
    console.log(data.Name);
    console.log(data.password);
 }

}