javascript - API 数组问题
问题描述
这是 API 响应:
{
"meals": [
{
"strTags":"Fish,Breakfast,DateNight",
}
]
}
我想看到以下结果:
这是我的代码:
<div>
{recipe.strTags ? <Chip label={recipe.strTags.split(",")} /> : null}
</div>
现在它看起来像这样:
我怎样才能做到这一点?
解决方案
您需要拆分字符串以创建数组,然后使用map对其进行迭代以创建每个标签。请记住添加key
属性以避免重新渲染问题。
解决方案:
<div>
{
recipe.strTags
? recipe.strTags
.split(',')
.map(label => (
<Chip label={label} key={label} />
))
: null
}
</div>
推荐阅读
- codeigniter - CodeIgniter 登录表单不会重定向到仪表板
- python - pip 将软件包安装在错误的目录中
- discord - 如何在 discord.py 的 cog 内创建后台任务?
- android - 有人知道这个Android类的功能吗?“com.google.apps.tiktok.tracing.db”?
- python - 将信息从 dics 列表解析到系列级别
- r - 如何提取 R 中 GJRM 包中 post.check 函数的 QQPLOT?
- python - LSTM 分类器的准确率低
- firebase - Firebase 分析每小时粒度报告
- sharepoint - 使用 CSOM 将 AD 组添加到 Sharepoint Online 组
- javascript - 为什么传递道具无限循环会使我的应用程序崩溃并且甚至不更新道具?