首页 > 解决方案 > 如何在“React Native Simple Store”中获取数组值?

问题描述

如何在“React Native Simple Store”中获取数组值?

这是我的代码:

const tryList = [];

store.get('shoppingList').then(res => (tryList = {res}));
console.log(tryList);

输出:

{
  "res": [{
    "price": 10,
    "name": "t-shirt"
  }]
}

预期输出:

[{
  "price": 10,
  "name": "t-shirt"
}]

标签: javascriptarraysreact-nativearraylistasyncstorage

解决方案


store.get('shoppingList').then(res => (tryList = {res}));
console.log(tryList)

看,当你打印tryList它时,你会得到一个对象{"res": [{"price": 10, "name": "t-shirt"}]}

但是您需要获取 so 的值,res以便您访问对象属性的方式类似,您必须在此处执行如下操作。

console.log(tryList.res)# 这将给出你预期的结果。


推荐阅读