2 回答
TA贡献1906条经验 获得超3个赞
const arr = ["2018-04-17T03", "2018-04-18T15", "2018-04-17T04", "2018-04-18T16", "2018-04-17T05", "2018-04-18T17", "2018-04-17T06", "2018-04-17T07", "2018-04-17T08", "2018-04-17T09", "2018-04-17T20", "2018-04-18T10", "2018-04-17T21", "2018-04-18T11", "2018-04-17T00", "2018-04-17T22", "2018-04-18T12", "2018-04-17T01", "2018-04-17T23", "2018-04-18T13", "2018-04-17T02", "2018-04-18T14", "2018-04-17T14", "2018-04-18T04", "2018-04-17T15", "2018-04-18T05", "2018-04-17T16", "2018-04-18T06", "2018-04-17T17", "2018-04-18T07", "2018-04-17T18", "2018-04-18T08", "2018-04-17T19", "2018-04-18T09", "2018-04-17T10", "2018-04-18T00", "2018-04-17T11", "2018-04-18T01", "2018-04-17T12", "2018-04-18T02", "2018-04-17T13", "2018-04-18T03"];
const prefix = '2018-04-17'; // 这个是相通的前缀,可以换
const samePrefix = []; // 存放相同前缀的数组
const others = []; // 存放不同前缀的数组
arr.forEach(date=>{
if (date.substring(0,10) === prefix) {
samePrefix.push(date)
} else {
others.push(date);
}
})
TA贡献1784条经验 获得超8个赞
var arr = ["2018-04-17T03", "2018-04-18T15", "2018-04-17T04", "2018-04-18T16", "2018-04-17T05", "2018-04-18T17", "2018-04-17T06", "2018-04-17T07", "2018-04-17T08", "2018-04-17T09", "2018-04-17T20", "2018-04-18T10", "2018-04-17T21", "2018-04-18T11", "2018-04-17T00", "2018-04-17T22", "2018-04-18T12", "2018-04-17T01", "2018-04-17T23", "2018-04-18T13", "2018-04-17T02", "2018-04-18T14", "2018-04-17T14", "2018-04-18T04", "2018-04-17T15", "2018-04-18T05", "2018-04-17T16", "2018-04-18T06", "2018-04-17T17", "2018-04-18T07", "2018-04-17T18", "2018-04-18T08", "2018-04-17T19", "2018-04-18T09", "2018-04-17T10", "2018-04-18T00", "2018-04-17T11", "2018-04-18T01", "2018-04-17T12", "2018-04-18T02", "2018-04-17T13", "2018-04-18T03"];
const result=arr.reduce((obj,item)=>{
const key=item.substring(0,10);
if(!obj[key]){
obj[key]=[];
}
obj[key].push(item);
return obj;
},{});
const _result=Object.keys(result).map(key=>result[key]);
console.log(_result);
添加回答
举报