Search code examples
typescriptangular7angular-components

How to return value of userPass from array of object using angular 7?


How to access value of userPass from array of object using angular 7?

I need to access Property userPass value from array of object

I have variable type any his name auth

auth have array of object

I need to access property value of userpass from json below

var auth;
auth =  


    [{"userLoginID":0,"userName":"test","userMail":"ahmedsa.aziz.ba@gmail.com","userPass":"12345678","fkTeamID":0  

    ,"isAdmin":false,"createdBy":0,"createdDate":"0001-01-01T00:00:00","isHidden":false}]  

I need to access userPass value meaning i need to access 12345678

what i try is

ngOnInit() {


     auth.forEach(element => {
      element.forEach(au => {
         console.log(au.userPass);

      });
      });
     userpathvalue=???
      }

Meaning i need to access value 12345678


Solution

  • Since length of your array is one, so then this code is enough to get value.

    auth[0].userPass

    So, If you do :

    console.log(auth[0].userPass)

    It will print "12345678"

    *Note: This solution is only for your specific case because you have the length of your array is one, if more then you have to loop through.