Greetings, I have received a JSON response.
response.data: [{
"id": 1,
"datefrom": "2018-08-30 11:21:25",
"dateto": "2018-08-31 11:21:25",
}, {
"id": 2,
"datefrom": "2018-08-30 11:21:25",
"dateto": "2018-08-31 11:21:25",
}, {
"id": 3,
"datefrom": "2018-09-01 11:21:25",
"dateto": "2018-09-03 11:21:25",
}, {
"id": 4,
"datefrom": "2018-09-02 11:21:25",
"dateto": "2018-09-03 11:21:25",
}, {
"id": 5,
"datefrom": "2018-09-03 11:21:25",
"dateto": "2018-09-04 11:21:25",
}]
I am looking to split this JSON data into two based on the 'datefrom' column if 'datefrom' < "2018-08-31 11:21:25".
The divided JSON data should look like this:
json1: [{
"id": 1,
"datefrom": "2018-08-30 11:21:25",
"dateto": "2018-08-31 11:21:25",
}, {
"id": 2,
"datefrom": "2018-08-30 11:21:25",
"dateto": "2018-08-31 11:21:25",
}]
and
json2:[{
"id": 3,
"datefrom": "2018-09-01 11:21:25",
"dateto": "2018-09-03 11:21:25",
}, {
"id": 4,
"datefrom": "2018-09-02 11:21:25",
"dateto": "2018-09-03 11:21:25",
}, {
"id": 5,
"datefrom": "2018-09-03 11:21:25",
"dateto": "2018-09-04 11:21:25",
}]
Would appreciate guidance on how to achieve this using JavaScript.
I have attempted the following:
if(datediff<120000){
values.push(response.data[i]['datefrom']);
Object.assign(values, {datefrom: response.data[i]['datefrom']});
values.push(response.data[i]['checkout']);
Object.assign(values, {dateto: response.data[i]['dateto']});
console.log(values)
$rootScope.expesscheckindata1= values;
else{
values1.push(response.data[i]['datefrom']);
Object.assign(values1, {datefrom: response.data[i]['datefrom']});
values1.push(response.data[i]['checkout']);
Object.assign(values1, {dateto: response.data[i]['dateto']});
console.log(values1)
$rootScope.expesscheckindata2= values1;
However, during the process, the data gets appended as the next data like ["2018-08-30 11:21:25", "2018-08-31 11:21:25", "2018-08-29 00:00:00", "2018-08-30 00:00:00", datefrom: "2018-08-29 00:00:00", dateto: "2018-08-30 00:00:00"]