I have an array of objects, from which I want to filter distinct movie titles and create another array of objects.
var movs = [
{
"Id": 3446,
"FilmId": "ST00000358",
"FilmPackageId": null,
"Title": "Harry Potter and the Goblet of Fire",
},
{
"Id": 3447,
"FilmId": "ST00000358",
"FilmPackageId": null,
"Title": "Harry Potter and the Goblet of Fire",
},
{
"Id": 3448,
"FilmId": "ST00000359",
"FilmPackageId": null,
"Title": "Harry Potter and the Order of the Phoenix",
},
{
"Id": 3449,
"FilmId": "ST00000360",
"FilmPackageId": null,
"Title": "Spider-Man: Into The Spider-Verse",
},
{
"Id": 3450,
"FilmId": "ST00000360",
"FilmPackageId": null,
"Title": "Spider-Man: Into The Spider-Verse",
}
]
I want to create an array of objects, which filter down to the distinct movie title and its id.
[
{
Title: 'Harry Potter and the Goblet of Fire',
FilmId: 'ST00000358'
},
{
Title: 'Harry Potter and the Order of the Phoenix',
FilmId: 'ST00000359'
},
{
Title: 'Spider-Man: Into The Spider-Verse',
FilmId: 'ST00000360'
}
]
I have successfully got an array of distinct titles, with the below, but if i add additional fields to the map, it returns the whole array, not the distinct few.
const movies = [...new Set(movs.map(item => item.Title))];
this does not work:
const movies = [
...new Set(
movs.map((obj) => ({ label: obj.Title, value: obj.FilmId }))
)
];