[英]Convert String to Object Key Name
I'm sending different objects to a component outside, and component data varies depending on objects. 我将不同的对象发送到外部组件,组件数据因对象而异。 I'm getting the names with the Object.key function because the keywords I send have different key.
我正在使用Object.key函数获取名称,因为我发送的关键字具有不同的键。 Then I want to sort by the key.
然后我想按键排序。 For this I need to define the name I received with Object.key function.
为此,我需要使用Object.key函数定义我收到的名称。 How can I do it?
我该怎么做?
upSortTable(items, val) {
//items = Object,
//val = index
let Keys = Object.keys(items[0]); // ["item_id","item_title"]
let keyname = Keys[val]; //item_id String value
//want to use in sort function as b.item_id
return items.sort(function(a, b) {
return b.keyname - a.keyname;
});
},
You'll need to use computed property : 您需要使用计算属性 :
return items.sort(function(a, b) {
return b[keyname] - a[keyname];
});
When you do a.keyname
you're actually looking for the property keyname
in a
itself. 当你做
a.keyname
你实际上是在寻找物业keyname
在a
本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.