Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to access value of userPass from array of object using angular 7?
I need to access Property userPass from array of object
I have variable from type any his name auth
auth have array of object
I need to access property value from userpass
auth is

[{"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
so what i do to make that please using angular 7

What I have tried:

const passusr = [...new Set(auth.map(au => au.userPass))]
Posted
Updated 11-Jan-20 21:43pm

1 solution

If auth is an array with only one object, this will work:
JavaScript
let pass = auth[0].userPass;

If auth has multiple objects:
JavaScript
let passwords = auth.map(a => a.userPass);
 
Share this answer
 
v2
Comments
ahmed_sa 12-Jan-20 4:53am    
thank you for reply
I have array have one object
id do as you told me above
ngOnInit()()
{
auth = localStorage.getItem('LoginUserData');
console.log("auth is" + localStorage.getItem('LoginUserData'));



let pass = auth[0].userPass;
console.log("check new" + pass);
}
result on browser
error undefined
How to solve this problem ?

auth is[{"userLoginID":0,"userName":"test","userMail":"ahmedsa.aziz.ba@gmail.com","userPass":"fayez123","fkTeamID":0,"isAdmin":false,"createdBy":0,"createdDate":"0001-01-01T00:00:00","isHidden":false}]
changepassword.component.ts:68 check newundefined
Thomas Daniels 12-Jan-20 4:56am    
Strange, at a first glance that should work... if you do console.log(auth[0]), does that say "undefined" or does it give the object?
ahmed_sa 12-Jan-20 5:05am    
console.log("check new" + auth[0]);
it give me
check new[
Thomas Daniels 12-Jan-20 5:09am    
Aha, then I think the problem is that your array is actually a JSON string rather than a JavaScript array. There are two solutions (choose one of them, not both!):

* when using localStorage.setItem to store LoginUserData, make sure that you're storing the array and not a string
* replace the first line of your function with: auth = JSON.parse(localStorage.getItem('LoginUserData'));
ahmed_sa 12-Jan-20 6:05am    
can you tell me what final code i modify to get access to value of userPass

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900