将两个对象合并为一个对象并按时间戳排序
Combining two objects into one object and sorting by timestamp
在 Laravel 中,我使用这种方法将集合组合在一起并作为一个集合返回。
$collection = $messages->merge($texts)->sortByDesc('created_at')
如果我dd($colection)
,它显示集合对象全部合并排序。
然后我尝试通过ajax将它发送到vue,但是,数据又被分离了。所以我的对象看起来像这样:
item: {
messages: [
0: { ... }
1: { ... }
2: { ... }
3: { ... }
],
texts: [
0: { ... }
1: { ... }
]
}
这是因为return response()->json('item' => '$collection')
再次将它们分为消息和文本。
我试过像这样组合它们,但它覆盖了值(我假设是因为 ID 相同)。
vm item = this;
// in response of ajax get,
.then(function(response) {
var item = response.data.item;
Object.assign(vm.item.messages, vm.item.texts);
});
将文本组合成消息并按时间戳排序的正确方法是什么?他们在第一级对象中都有created_at这样的:
messages: [
0: { created_at: ... }
],
texts: [
0: { created_at: ... }
]
更新:在 icepickle 的回答之后,通过 concat,我能够将它们组合到消息数组中。现在,我遇到 created_at 值的问题,因为它们被转换为字符串。下面是一些测试数据。这是我订购后得到的:
messages: [
0: {
msg: 'hello',
created_at: "2017-10-12 00:48:59"
},
1: {
msg: 'mellow',
created_at: "2017-10-11 16:05:01"
},
2: {
msg: 'meow',
created_at: "2017-10-11 15:07:06"
},
4: {
msg: 'test'
created_at: "2017-10-11 17:13:24"
}
5: {
msg: 'latest'
created_at: "2017-10-12 00:49:17"
}
],
数组concat
然后排序还不够吗?
有点像
let result = ([]).concat(item.messages, item.texts);
或 es6
let result = [...item.messages, ...item.texts]
然后对结果调用排序
// in place sort, result will be sorted, and return the sorted array
result.sort((a, b) => a.created_at - b.created_at);
const items = {
messages: [
{
msg: 'hello',
created_at: "2017-10-12 00:48:59"
},
{
msg: 'mellow',
created_at: "2017-10-11 16:05:01"
},
{
msg: 'meow',
created_at: "2017-10-11 15:07:06"
}
],
texts: [
{
msg: 'test',
created_at: "2017-10-11 17:13:24"
},
{
msg: 'latest',
created_at: "2017-10-12 00:49:17"
}
]
};
let result = [...items.messages, ...items.texts].sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
console.log( result );
在 Laravel 中,我使用这种方法将集合组合在一起并作为一个集合返回。
$collection = $messages->merge($texts)->sortByDesc('created_at')
如果我dd($colection)
,它显示集合对象全部合并排序。
然后我尝试通过ajax将它发送到vue,但是,数据又被分离了。所以我的对象看起来像这样:
item: {
messages: [
0: { ... }
1: { ... }
2: { ... }
3: { ... }
],
texts: [
0: { ... }
1: { ... }
]
}
这是因为return response()->json('item' => '$collection')
再次将它们分为消息和文本。
我试过像这样组合它们,但它覆盖了值(我假设是因为 ID 相同)。
vm item = this;
// in response of ajax get,
.then(function(response) {
var item = response.data.item;
Object.assign(vm.item.messages, vm.item.texts);
});
将文本组合成消息并按时间戳排序的正确方法是什么?他们在第一级对象中都有created_at这样的:
messages: [
0: { created_at: ... }
],
texts: [
0: { created_at: ... }
]
更新:在 icepickle 的回答之后,通过 concat,我能够将它们组合到消息数组中。现在,我遇到 created_at 值的问题,因为它们被转换为字符串。下面是一些测试数据。这是我订购后得到的:
messages: [
0: {
msg: 'hello',
created_at: "2017-10-12 00:48:59"
},
1: {
msg: 'mellow',
created_at: "2017-10-11 16:05:01"
},
2: {
msg: 'meow',
created_at: "2017-10-11 15:07:06"
},
4: {
msg: 'test'
created_at: "2017-10-11 17:13:24"
}
5: {
msg: 'latest'
created_at: "2017-10-12 00:49:17"
}
],
数组concat
然后排序还不够吗?
有点像
let result = ([]).concat(item.messages, item.texts);
或 es6
let result = [...item.messages, ...item.texts]
然后对结果调用排序
// in place sort, result will be sorted, and return the sorted array
result.sort((a, b) => a.created_at - b.created_at);
const items = {
messages: [
{
msg: 'hello',
created_at: "2017-10-12 00:48:59"
},
{
msg: 'mellow',
created_at: "2017-10-11 16:05:01"
},
{
msg: 'meow',
created_at: "2017-10-11 15:07:06"
}
],
texts: [
{
msg: 'test',
created_at: "2017-10-11 17:13:24"
},
{
msg: 'latest',
created_at: "2017-10-12 00:49:17"
}
]
};
let result = [...items.messages, ...items.texts].sort((a, b) => new Date(a.created_at) - new Date(b.created_at));
console.log( result );