John Doe
John Doe

Reputation: 83

How to take a specific value from an array?

This is how I set the data to the storage

this.storage.set('userData', JSON.stringify(this.responseData) )


The data will look like this when saved to the indexedDB

[{"user_id":1,"username":"admin"...}]

The problem is if I extract the array it only takes 1 character

this.storage.get('userData').then((res) => { console.log(res)
  console.log(res[3])
});

Like this code above. The res[3] will only take the letter u
I wanted to take the name and the value like for example if I call the res.user_id it will give me 1 or res.username to admin

Upvotes: 2

Views: 46

Answers (1)

CodeHacker
CodeHacker

Reputation: 2138

Since you did a JSON.stringify... the data is stored now as a string.. so you're taking now the fourth letter of the string "[{\"user... " which is the "u" you get. Simply make a JSON.parse(res) first... and then you should get the desired results

this.storage.get('userData').then((res) => { 
 console.log(res)
 var users =JSON.parse(res) 
 console.log(users[0].username)
 });

Upvotes: 2

Related Questions