javascript - 如何将参数传递给 const 对象变量
问题描述
给定
onColumnResize(column) {
const columnsWidth = {
columnsWidth: {
column: width + 'px'
},
detail: this.details
};
}
如何将参数列传递给对象 columnsWidth 变量?例如:如果参数 column = 'name',想将 'name' 传递给 column 并输出以下内容(取决于函数的参数):
onColumnResize(column) {
const columnsWidth = {
columnsWidth: {
'name': width + 'px'
},
detail: this.details
};
}
我的尝试:`
onColumnResize(column) {
const columnsWidth = {};
columnsWidth[column] = width+'px';
columnsWidth.detail = this.details;
}`
除了上述方法还有其他方法吗?
解决方案
在您的第一个代码块中更改column
为:[column]
onColumnResize(column) {
const columnsWidth = {
columnsWidth: {
[column]: width + 'px'
},
detail: this.details
};
// ...
}
这称为计算属性名称,它是 ES2015 的一部分,而不是 ES2016。
推荐阅读
- r - R聚合data.frame在一列中有日期和小时的格式错误
- llvm - 对“llvm::EnableABIBreakingChecks”的未定义引用
- reactjs - Expo TaskManager API:在“导出默认值”之外更新状态
- angular - 一个新的 Angular7 项目(没有改变)无法运行,编译成功但无法在 4200 端口(chrome)上打开
- sql - 创建显示客户订单的程序:各个年份的汇总数量和金额订单。程序输入时的客户 ID
- java - 将日+月转换为最近的匹配日期
- neural-network - 如何在 tensorflow.js 中创建自己的图层
- c++ - 第 933 行:字符 34:运行时错误:引用绑定到类型为“struct value_type”(stl_vector.h)的空指针 - leet 代码螺旋
- java - 在网页中下载图像时出现错误 403
- c - 使用C中的函数的内积