How do I extract the common elements from an array and put them into two arrays?
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"];
The last three digits of each element in
arr are different. I want to intercept the first ten digits of each element into an array and put different ones into another array
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);
}
})
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);