我有一个由多个数组组成的对象,这些数组共享时间序列中事件的公共(public)属性:
{
time: [1522513800000, 1522515600000, 1522517400000, 1522519200000]
event: ['foo', 'bar', 'some', 'thing']
notes: ['', 'not foo', 'sum', '']
}
我想将这个对象重新排序(重新排列?)到相应值的数组中,如下所示:
[[1522513800000, 'foo', ''], [1522515600000, 'bar', 'not foo'], [1522517400000, 'some', 'sum'], [1522519200000, 'thing', '']]
我更喜欢普通/ES6 解决方案,但如果可行的话,我将在这个项目中使用 lodash。
最佳答案
先转为数组,然后使用.sort
:
const input = {
time: [1522513800000, 1522515600000, 1522517400000, 1522519200000],
event: ['foo', 'bar', 'some', 'thing'],
notes: ['', 'not foo', 'sum', '']
};
const numItems = input.time.length;
const items = Array.from({ length: numItems }, (_, i) => ([
input.time[i],
input.event[i],
input.notes[i],
]));
items.sort((itemA, itemB) => itemB.time - itemA.time);
console.log(items);
关于javascript - 重新排序数组对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49765496/