0

I'm trying to create an array from this localStorage data. This because I want to bind the data to a list in my HTML doc. Since <li *ngFor="let user of users">{{user.name}}</li> only supports arrays I need to convert my JSON to an array.

LocalStorage JSON:

Key: User Value: {"name":"Kevin","country":"Canada","about":"Test","image":""}
1
  • Why don't you store it as array? Commented Sep 21, 2017 at 7:43

2 Answers 2

2

Create a variable named users

users:any = [];

then

let user = JSON.parse(localStorage.getItem('user'));
this.users.push(user);
Sign up to request clarification or add additional context in comments.

2 Comments

I get this error ERROR TypeError: Cannot read property 'name' of null
try this <li *ngFor="let user of users">{{user?.name}}</li>
2

You can do it using map function:

var users = Object.keys(obj).map(function(x) { return obj[x] });

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.