首先看看这玩意

当js对象的属性是一个变量_添加属性

//定义对象
let person={};
//为对象添加属性
person['age']=18
//为对象添加属性(属性为参数变量)
let temp='name'
person[temp]='jary'
console.log(person)
//输出结果
//Object {age: 18, name: "jary"}

下边是我在实现一个表格排序的时候遇到的,后端哥哥不给排序接口…那就自己整一个排序噻,数据还是比较简单.

this.state.tableListTest 的数据

当js对象的属性是一个变量_数据_02

const columnKey = `${sorter.columnKey}`
if (sorter.order == 'ascend' && (columnKey == 'classlineDay' || columnKey == 'deviceRunDay')) {
let newarr = this.state.tableListTest.sort(function (a, b) {
return b[columnKey] < a[columnKey] ? -1 : 1
})
this.setState({ tableListTest: newarr })
}

我这里​​columnKey​​​就是一个变量,根据​​sorter.columnKey​​值得不同去去到对象中不同的值来排序.