I have an array of objects having multiple arrays
const arr = [
{
JUNE: [
{
"iD": "67854",
"event": " closed",
"title": "test",
"startDate": "2024-06-20",
"endDate": "2024-07-25"
}
]
},
{
MAY: [
{
"ID": "908765",
"event": "closed",
"title": "test",
"startDate": "2024-05-21",
"endDate": "2024-06-27"
},
{
ID: "12345",
event: "open",
title: "test123",
startDate: "2024-05-21",
endDate: "2024-06-27"
}
]
}
]
Output I need
[
{
ID: "67854",
event: "closed",
title: "test",
startDate: "2024-06-20",
endDate: "2024-07-25"
},
{
ID: "908765",
event: "closed",
title: "test",
startDate: "2024-05-21",
endDate: "2024-06-27"
},
{
ID: "12345",
event: "open",
title: "test123",
startDate: "2024-05-21",
endDate: "2024-06-27"
}
]
4
Answers
Based on the image you’ve provided, which shows the desired output format for your data transformation, it appears you want to convert an array of objects that represent events.
One possible way to do it (not the most readable if you’re not familiar with those) is to use
reduce
andconcat
like this:reduce
allows you to loop over an array and to aggregate data into an accumulator. In this case, the accumulator is your result array.Another option is to
reduce
the array andflatMap
eachObject.keys
with an deepermap
for all the events.Or as an one-liner":
You can use a reduce function to flatten your function: