我有以下数据:
phones: {
'home': {
number: '1234567890',
primary: false,
preferred: false
},
'mobile': {
number: '2134567890',
primary: true,
preferred: true
},
'work': {
number: '1234567809',
primary: false,
preferred: true
}
}
我将它传递给React组件:
render: function() {
return (
<MyComponent data={
Object.keys(phones)
.reduce((prev, key) => [...prev, phones[key]], []) // omitting keys
.sort(this.sortPhoneNumbers)
}
/>
)
}
这是我的自定义排序功能:
sortPhoneNumbers: function (a, b) {
if (a.preferred === b.preferred) {
return (b.primary - a.primary)
} else if (a.preferred < b.preferred) {
return 1
} else if (a.preferred > b.preferred) {
return -1
} else {
return 0
}
}
我想使用Lodash sortBy function代替。
答案 0 :(得分:0)
您可以将其应用于从Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from vpython import *
>>> box()
<vpython.vpython.box object at 0x0497E770>
>>>
喜欢
Object.keys().reduce