根据日期使用javascript组合json数组
嗨,我正在做一个网站 我有两个Json对象数组,这些数据如下所示 (这些都有价值,但除了本例中的数据之外,我已经隐藏了所有这些) 下一个看起来像根据日期使用javascript组合json数组,javascript,reactjs,Javascript,Reactjs,嗨,我正在做一个网站 我有两个Json对象数组,这些数据如下所示 (这些都有价值,但除了本例中的数据之外,我已经隐藏了所有这些) 下一个看起来像 alertId: " " categoryId: " " countries: null countryId: " " createdBy: " " createdDate: "2020-10-16T09:12:20.957Z" customerId:
alertId: " "
categoryId: " "
countries: null
countryId: " "
createdBy: " "
createdDate: "2020-10-16T09:12:20.957Z"
customerId: null
customerName: " "
deletedBy: null
deletedDate: null
description: "Images released on social media ."
email: null
endDate: "2020-10-17T10:06:31.81"
firstname: null
formattedAddress: ""
homeCountryId: null
isDeleted: false
itemId: null
keywordMatches: null
keywordsMatched: 0
latitude: 13.7462
link: null
longitude: 100.531
notes: "Avoid ."
phone: null
riskId: "6 "
smsEnabled: true
sourceId: " "
startDate: "2020-10-16T10:06:31.81"
surname: null
title: "Update: Protesters gather "
travellerTagId: null
所以两者已经非常相似了
基本上我有两个页面,每个页面都有一个单独的数组
所以我有一个组件
const rendertravelAlerts = this.props.travelAlerts.map((todo, index) => {
return (
<li key={index}>
<div>
travel Title {todo.title}
<li>t ID {todo.id}</li>
<li>t Id {todo.userId}</li>
</div>
</li>
);
});
const rendertravelAlerts=this.props.travelAlerts.map((todo,索引)=>{
返回(
旅行标题{todo.Title}
t ID{todo.ID}
t Id{todo.userId}
);
});
另一方面呢
const renderTodos = currentTodos.map((todo, index) => {
return (
<li key={index}>
<div>
intel Title {todo.title}
<li>Tintel ID {todo.id}</li>
<li>intel ID {todo.userId}</li>
</div>
</li>
);
});
const rendertoos=currentTodos.map((todo,索引)=>{
返回(
英特尔标题{todo.Title}
Tintel ID{todo.ID}
英特尔ID{todo.userId}
);
});
现在我正在制作第三个组件,它将显示这两种类型的警报
我通过将这两个数组的道具传递给这个组件,然后像以前一样映射它们来实现这一点
我的问题是因为有一个。地图,然后其他项目是不混合的,这是我想要的
理想情况下,我可以根据日期混合这两个数组,然后使用单个.map来显示它们。但我不知道该怎么做。据我所知,您有两个数组:
array\u 1和array\u 2
,您希望合并它们,然后按日期对结果排序
如果是,要合并它们,可以使用.concat()
方法:
array_1.concat(array_2);// this will return a new array containing all elements for both array_1 & array_2
array_3.sort((a, b) => {
return new Date(a.startDate) - new Date(b.startDate)
});
注意:
如果要在阵列中添加不重复的阵列,可以执行以下操作:
[...array_1, ...array_2]
array_1.concat(array_2).sort((a, b) => {
return new Date(a.startDate) - new Date(b.startDate)
});
然后,假设array\u 1.concat(array\u 2)
将产生一个新的数组:array\u 3
,要对array\u 3的项目进行排序,可以使用.sort()
方法:
array_1.concat(array_2);// this will return a new array containing all elements for both array_1 & array_2
array_3.sort((a, b) => {
return new Date(a.startDate) - new Date(b.startDate)
});
要简化,您可以执行以下操作:
[...array_1, ...array_2]
array_1.concat(array_2).sort((a, b) => {
return new Date(a.startDate) - new Date(b.startDate)
});
“基于日期”。。。哪个日期?你能给我们一个预期结果的例子吗?E是创建日期和开始日期两者都可以很好地工作预期结果只是这两种类型的项目按日期排序的数组。你想按日期排序组件还是简单地按数据排序?宾果。这工作做得很好!