javascript - 如何渲染对象内的对象数组反应本机?
问题描述
这是我的 JSON:
{
"products": [
{
"_id": "5fc9027dd3b8f63398ae8132",
"name": "Oriz 500g",
"quantity": 1
}
],
"status": "CANCELLED",
"total": 0,
}
我需要从产品中呈现名称和数量。我正在渲染这样,但它不起作用
<Text style={styles.listProduct}>{data.products.name}</Text>
注:数据来自
const [data, setData] = useState([]);
const response = await axiosApiInstance.get(
`/orders/get-order-details/5fce356c0fbc3605bffe2060`
);
setData(response.data);
解决方案
您需要在数组中循环并提取所需的数组项,请遵循此
<View>
{data.length > 0 && data.products.map((element, index) => {
return (
<View>
<Text style={styles.listProduct}>Name: {element.name}</Text>
<Text style={styles.listProduct}>Quantity: {element.quantity}</Text>
</View>
);
})}
</View>
根据您的要求修改
推荐阅读
- http - 从 HttpURLConnection 获取实体主体
- google-chrome - 如何以完全相同的构建顺序查看由 Web 浏览器(Firefox、Chrome、Safari)触发的 HTTP 标头
- javascript - 我的 webpack 捆绑脚本中未定义的节点模块
- java - Activity是MainFragmentListener的一个实例,是真的吗?
- css - 引导列在 Chrome 中没有对齐属性(在 Firefox 中也是如此)。我究竟做错了什么?
- kivy - 防止 KIVY 启动全屏
- laravel - 实现 Laravel Eloquent 模型继承
- java - 动态更改 JavaFX css 属性
- mfcc - 如何将 MFCC 提交给输入算法?
- java - 递归选择排序Java