我想拆分这个对象数组。这只是整个阵列的一部分,其中包含未来两年的预订。
[ { state: 'BOOKED', date: 2017-01-01T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-02T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-03T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-04T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-05T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-06T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-27T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-28T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-29T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-30T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-31T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-01T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-02T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-03T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-04T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-05T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-06T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-07T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-08T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-09T23:00:00.000Z } ]
按连续日期分组的数组,如:
[ [ { state: 'BOOKED', date: 2017-01-01T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-02T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-03T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-04T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-05T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-06T23:00:00.000Z } ],
[ { state: 'BOOKED', date: 2017-01-27T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-28T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-29T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-30T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-01-31T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-01T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-02T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-03T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-04T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-05T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-06T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-07T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-08T23:00:00.000Z },
{ state: 'BOOKED', date: 2017-02-09T23:00:00.000Z } ] ]
我现在发疯了。更新:
到目前为止我尝试了什么:
var _ = require('lodash');
var moment = require('moment');
var bookings = [
{state: 'BOOKED', date: moment('2017-01-01T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-02T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-03T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-04T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-05T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-06T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-27T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-28T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-29T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-30T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-01-31T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-01T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-02T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-03T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-04T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-05T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-06T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-07T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-08T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-09T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-18T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-19T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-20T23:00:00.000Z')},
{state: 'BOOKED', date: moment('2017-02-21T23:00:00.000Z')}
];
var ranges = {};
var range = 1;
_.each(bookings, function (booking, idx) {
var next = idx + 1;
if (!ranges[range]) {
ranges[range] = [];
}
if (!bookings[next]) {
return;
}
if (booking.date.clone().add(1, 'day').isSame(bookings[next].date)) {
ranges[range].push(booking);
} else {
range++;
}
});
哪种工作方式,但前提是数组内的对象的顺序正确。
答案 0 :(得分:1)
您可以使用Array.reduce
函数循环数据并匹配当前值和下一个值。
如果差值为1,则将其推入临时数组。重置临时数组并将当前元素推入其中。
如果没有,并且临时数组有值,请在最终数组中推送临时数组。
var data=[{state:"BOOKED",date:"2017-01-01T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-02T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-03T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-04T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-05T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-06T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-27T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-28T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-29T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-30T23:00:00.000Z"},{state:"BOOKED",date:"2017-01-31T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-01T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-02T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-03T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-04T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-05T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-06T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-07T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-08T23:00:00.000Z"},{state:"BOOKED",date:"2017-02-09T23:00:00.000Z"}];
var result = [];
var lastDate = null;
data.reduce(function(p, c, i, a){
var date = new Date(c.date);
if (!(lastDate === null || getDiffInDays(lastDate, date) === 1)){
result.push(p);
p=[];
}
p.push(c)
if(i === a.length - 1 && p.length > 0){
result.push(p);
}
lastDate = date;
return p;
},[])
console.log(result)
function getDiffInDays(d1, d2){
if(d1 && d2){
var milliSecInDay = 24 * 60 * 60 * 1000;
return parseInt((+d2 - +d1)/milliSecInDay);
}
}