I have a array as follows:
[
{
"values": [
{
"title": "Status",
"text": "closed"
},
{
"title": "Timeline",
"text": "2021-12-06 - 2021-12-24"
}
]
},
{
"values": [
{
"title": "Status",
"text": "overdue"
},
{
"title": "Timeline",
"text": "2021-12-06 - 2021-12-24"
}
]
},
{
"values": [
{
"title": "Status",
"text": "open"
},
{
"title": "Timeline",
"text": "2021-12-06 - 2021-12-24"
}
]
},
{
"values": [
{
"title": "Status",
"text": "open"
},
{
"title": "Timeline",
"text": "2021-12-06 - 2021-12-24"
}
]
},
{
"values": [
{
"title": "Status",
"text": "closed"
},
{
"title": "Timeline",
"text": "2021-12-06 - 2022-01-29"
}
]
}
]
I want my final output in three arrays closedArray,openArray,dueArray. I want to traverse this array, go to status field, check status. if status is closed, then put element in closedArray, if overdue then put element in due array, if open put element in open array. If date in Timeline field passed the current date, then need to put that element in openArray. How can I do this?
Hey please check this out:
const results = {};
const array=[{values:[{title:"Status",text:"closed"},{title:"Timeline",text:"2021-12-06 - 2021-12-24"}]},{values:[{title:"Status",text:"overdue"},{title:"Timeline",text:"2021-12-06 - 2021-12-24"}]},{values:[{title:"Status",text:"open"},{title:"Timeline",text:"2021-12-06 - 2021-12-24"}]},{values:[{title:"Status",text:"open"},{title:"Timeline",text:"2021-12-06 - 2021-12-24"}]},{values:[{title:"Status",text:"closed"},{title:"Timeline",text:"2021-12-06 - 2022-01-29"}]}];
array.forEach(e => (
new Date(e.values[1].text.split(" - ")[1]) > new Date() ? // check if date has passed
results["open"]?.push(e) || (results["open"] = [e]) : // if yes -> add to open array
results[e.values[0].text]?.push(e) || (results[e.values[0].text] = [e]) // else add to other key
));
const {open, closed, overdue} = results;
console.log("openArray", open);
console.log("closedArray", closed);
console.log("overdueArray", overdue);