我想对以下产品进行分组:-
const array = [{date: "10 feb", from: "2", to: "10", name:"chair"},
{date: "10 feb", from: "10", to: "22", name: "chair"},
{date: "10 feb", from: "22", to: "31", name: "chair"},
{date: "10 feb", from: "35", to: "40", name: "chair"},
{date: "12 feb", from: "2", to: "10", name: "chair"},
{date: "12 feb", from: "10", to: "20", name: "toy"}]
例如,如果连续产品的日期和名称相似,并且第一个产品的“开始”与下一个产品的“结束”相同,则应将第一个产品的“开始”与下一个产品的“结束”进行分组。
Result array = [{date: "10 feb", from: "2", to: "31", name:"chair"},
{date: "10 feb", from: "35", to: "40", name: "chair"},
{date: "12 feb", from: "2", to: "10", name: "chair"},
{date: "12 feb", from: "10", to: "20", name: "toy"}]
最好使用Foreach循环而不是for循环。
1条答案
按热度按时间xhv8bpkk1#
利用
reduce
函数来实现所需的结果。希望这能帮上忙谢谢!