reactjs - 映射内部映射以呈现与外部映射函数标题匹配的数据
问题描述
我有一个 JSON 文件,如下所示:
[
{
"category": "food",
"subcategory": "Snacks",
"eng-name": "Eng name here",
"thai-name": "thai name here",
"description": "Food Description here",
"price": "price per dish or glass",
"price2": "price per bottles",
"tags": "[tags to find type of food or drink (Chicken, vegetarian, vegan, whisky, red wine, chardonnay)]",
"image": "url of img in upload in server"
},
{
"category": "food",
"subcategory": "Starters",
"eng-name": "Eng name here",
"thai-name": "thai name here",
"description": "Food Description here",
"price": "price per dish or glass",
"price2": "price per bottles",
"tags": "[tags to find type of food or drink (Chicken, vegetarian, vegan, whisky, red wine, chardonnay)]",
"image": "url of img in upload in server"
},
{
"category": "food",
"subcategory": "Snacks",
"eng-name": "Eng name here",
"thai-name": "thai name here",
"description": "Food Description here",
"price": "price per dish or glass",
"price2": "price per bottles",
"tags": "[tags to find type of food or drink (Chicken, vegetarian, vegan, whisky, red wine, chardonnay)]",
"image": "url of img in upload in server"
}
]
我想要做的是渲染每个子类别并在里面映射以渲染一张卡片,其中每个菜的详细信息对应于每个子类别的标题。请参阅此链接中的示例: 组件想要的图像
最好的方法是什么?
感谢您的回答。
解决方案
您可以使用underscore.js
's groupBy 函数将数据与子类别分组。当您获取数据时,您可以使用 map on 来呈现卡片详细信息。
var data = [
{
"category": "food",
"subcategory": "Snacks",
"eng-name": "Eng name here",
"thai-name": "thai name here",
"description": "Food Description here",
"price": "price per dish or glass",
"price2": "price per bottles",
"tags": "[tags to find type of food or drink (Chicken, vegetarian, vegan, whisky, red wine, chardonnay)]",
"image": "url of img in upload in server"
},
{
"category": "food",
"subcategory": "Starters",
"eng-name": "Eng name here",
"thai-name": "thai name here",
"description": "Food Description here",
"price": "price per dish or glass",
"price2": "price per bottles",
"tags": "[tags to find type of food or drink (Chicken, vegetarian, vegan, whisky, red wine, chardonnay)]",
"image": "url of img in upload in server"
},
{
"category": "food",
"subcategory": "Snacks",
"eng-name": "Eng name here",
"thai-name": "thai name here",
"description": "Food Description here",
"price": "price per dish or glass",
"price2": "price per bottles",
"tags": "[tags to find type of food or drink (Chicken, vegetarian, vegan, whisky, red wine, chardonnay)]",
"image": "url of img in upload in server"
}
];
console.log(_.groupBy(data,'subcategory'))
<script src="https://cdn.jsdelivr.net/npm/underscore@1.13.1/underscore-umd-min.js"></script>
推荐阅读
- linux - 如何从一行中搜索和删除模式?
- java - 如何删除 Java 代码中未处理的异常错误
- r - 如何使用一组正负一元和二元计算字符串中的单词?
- phpmyadmin - 从 config.inc.php 文件更改设置后,我无法登录我的 phpMyAdmin 帐户
- c# - 带有表达式变量的 Linq OrderBy
- django - 创建数据库后如何执行 docker-entrypoint-initdb.d/init.sql 文件?
- javascript - 如何在回调中使用它?
- sql - 子查询条件 (SQL)
- sql - 如何获得一个列的组的降序列表,该列是该组的另一个列的总和?
- python - 使用 python os.walk 如何检查目录名称并仅处理特定目录中的那些文件(递归)?