reactjs - 使用变量获取道具内部的值
问题描述
有没有人尝试使用变量在道具中获取特定值?
通常,在道具中获取特定数据就像
const dataValue = props.table.data.rowData.account_number
但是在这种情况下,我需要道具的最后一部分是一个变量,因为account_number
值不是固定的并且名称会有所不同。
那么有没有办法让我使用变量而不是在 prop 调用的过去部分添加固定名称?
像这样:
let theVariable = *somethingNew*;
const dataValue = props.table.data.rowData.theVariable
解决方案
您可以通过属性括号访问来做到这一点:
let theVariable = somethingNew;
const dataValue = props.table.data.rowData[theVariable]
这也可以在对象创建期间使用:
const dataValue = {
[theVariable]: {}
}
推荐阅读
- c++ - 如何在 CMake 文件中为 CLion IDE 链接 gdal 共享库?
- ios - 如何在 xamarin iOS 中基于父视图边界剪辑多个图像?
- node.js - 如何跟踪收件人是否单击电子邮件中的链接?
- circleci - 我们如何在当前构建完成后触发下一个作业
- python - 如何使用 Python 列出新的 TCP 连接?
- wiremock - 如何在 WireMock 的 ResponseBody 中传递 json 响应
- wordpress - 如何仅将旧域中的帖子重定向到新域中的相同帖子?.htacess?
- java - ExceptionHandlerExceptionResolver 在 Spring 5 中总是返回 200 响应码
- android - 如何为捕获的图像创建单独的私人文件夹?
- python - 如何编码 RSA 私钥以减少