I’d like to create a section list in react native, but I have to organize my REST API JSON response.
Here is my JSON
{
"movies": [
{ "id": "1", "title": "Star Wars", "releaseYear": "1990" },
{ "id": "2", "title": "Back to the Future", "releaseYear": "1990" },
{ "id": "3", "title": "The Matrix", "releaseYear": "2010" },
{ "id": "4", "title": "Inception", "releaseYear": "2010" },
{ "id": "5", "title": "Interstellar", "releaseYear": "2010" }
]
}
I’d like to obtain a structure like this
{
"movies": [
{
"releaseYear": "1990", "data": [
{ "id": "1", "title": "Star Wars"},
{ "id": "2", "title": "Back to the Future"}
]
},
{
"releaseYear": "2010", "data": [
{ "id": "3", "title": "The Matrix" },
{ "id": "4", "title": "Inception" },
{ "id": "5", "title": "Interstellar" }
]
}
}
If I use reduce, I cannot obtain the structure I’d like to have (this is because I just "group by" elements and I’m not able to move the "releaseYear" property and create new object array with filtered properties)
TY in advance!
4
Answers
Use a reducer, try to get the release year group and add the data to the array. If it does not exist add the release year and add the movie to the data array.
You can use
Array.reduce()
to group the movies byreleaseYear
, creating a data array for each year.This creates an mapping object with a property for each year, we can then use
Object.values()
to get the result as an array.We assign this to a
movies
property in the result object.