如何从数组中获取特定值?
How to take a specific value from an array?
这是我将数据设置到存储的方式
this.storage.set('userData', JSON.stringify(this.responseData) )
数据保存到indexedDB后会是这样
[{"user_id":1,"username":"admin"...}]
问题是如果我提取数组它只需要 1 个字符
this.storage.get('userData').then((res) => { console.log(res)
console.log(res[3])
});
喜欢上面这段代码。 res[3]
只会取字母 u
我想获取名称和值,例如如果我调用 res.user_id
它将给我 1
或 res.username
到 admin
因为你做了 JSON.stringify...数据现在存储为字符串..所以你现在取字符串的第四个字母 "[{\"user... " 这是你得到的"u"。
简单地先做一个 JSON.parse(res)...然后你应该得到想要的结果
this.storage.get('userData').then((res) => {
console.log(res)
var users =JSON.parse(res)
console.log(users[0].username)
});
这是我将数据设置到存储的方式
this.storage.set('userData', JSON.stringify(this.responseData) )
数据保存到indexedDB后会是这样
[{"user_id":1,"username":"admin"...}]
问题是如果我提取数组它只需要 1 个字符
this.storage.get('userData').then((res) => { console.log(res)
console.log(res[3])
});
喜欢上面这段代码。 res[3]
只会取字母 u
我想获取名称和值,例如如果我调用 res.user_id
它将给我 1
或 res.username
到 admin
因为你做了 JSON.stringify...数据现在存储为字符串..所以你现在取字符串的第四个字母 "[{\"user... " 这是你得到的"u"。 简单地先做一个 JSON.parse(res)...然后你应该得到想要的结果
this.storage.get('userData').then((res) => {
console.log(res)
var users =JSON.parse(res)
console.log(users[0].username)
});