0

I've researched about it, i tried these so far :

Merge javascript objects in array with same key
Knex/SQL: Merge one to many join in one object
Merge objects with same id in array

But i couldn't success to merge.

Here is my data :

const data = [
  {
    "id": 1,
    "movie_title": "Extraction",
    "imdb": 6.7,
    "length": "1h 56min",
    "movie_image": "https://i.imgur.com/JljBVho.jpg",
    "director": "Sam Hargrave",
    "stars": "Chris Hemsworth, Bryon Lerum, Ryder Lerum",
    "total_season": null,
    "production_year": 2020,
    "statu_title": "movie",
    "genre_title": "action"
  },
  {
    "id": 1,
    "movie_title": "Extraction",
    "imdb": 6.7,
    "length": "1h 56min",
    "movie_image": "https://i.imgur.com/JljBVho.jpg",
    "director": "Sam Hargrave",
    "stars": "Chris Hemsworth, Bryon Lerum, Ryder Lerum",
    "total_season": null,
    "production_year": 2020,
    "statu_title": "movie",
    "genre_title": "thriller"
  },
  {
    "id": 2,
    "movie_title": "The Old Guard",
    "imdb": 6.7,
    "length": "2h 5min",
    "movie_image": "https://i.imgur.com/PSYB2RK.jpg",
    "director": "Gina Prince-Bythewood",
    "stars": "Charlize Theron, KiKi Layne, Matthias Schoenaerts",
    "total_season": null,
    "production_year": 2020,
    "statu_title": "movie",
    "genre_title": "action"
  },
  {
    "id": 2,
    "movie_title": "The Old Guard",
    "imdb": 6.7,
    "length": "2h 5min",
    "movie_image": "https://i.imgur.com/PSYB2RK.jpg",
    "director": "Gina Prince-Bythewood",
    "stars": "Charlize Theron, KiKi Layne, Matthias Schoenaerts",
    "total_season": null,
    "production_year": 2020,
    "statu_title": "movie",
    "genre_title": "adventure"
  },
  {
    "id": 2,
    "movie_title": "The Old Guard",
    "imdb": 6.7,
    "length": "2h 5min",
    "movie_image": "https://i.imgur.com/PSYB2RK.jpg",
    "director": "Gina Prince-Bythewood",
    "stars": "Charlize Theron, KiKi Layne, Matthias Schoenaerts",
    "total_season": null,
    "production_year": 2020,
    "statu_title": "movie",
    "genre_title": "fantasy"
  }
]

And what i want to do is :

const data = [
  {
    "id": 1,
    "movie_title": "Extraction",
    "imdb": 6.7,
    "length": "1h 56min",
    "movie_image": "https://i.imgur.com/JljBVho.jpg",
    "director": "Sam Hargrave",
    "stars": "Chris Hemsworth, Bryon Lerum, Ryder Lerum",
    "total_season": null,
    "production_year": 2020,
    "statu_title": "movie",
    "genre_title": ["action","thriller"]
  },
  {
    "id": 2,
    "movie_title": "The Old Guard",
    "imdb": 6.7,
    "length": "2h 5min",
    "movie_image": "https://i.imgur.com/PSYB2RK.jpg",
    "director": "Gina Prince-Bythewood",
    "stars": "Charlize Theron, KiKi Layne, Matthias Schoenaerts",
    "total_season": null,
    "production_year": 2020,
    "statu_title": "movie",
    "genre_title": ["action","adventure","fantasy"]
  }
]

So basically i want to merge objects by id, and convert genre_title to an array. Actually I couldn't understand reduce and filter methods even though I researched about them. (Probably I don't even need lodash...) Thank you!

Oxibital
  • 135
  • 1
  • 2
  • 10

1 Answers1

2

You could create a Map, keyed by id, with an object that has the new object structure (with an empty genre_title array). Then you would iterate your data to populate that new array. Finally extract the values from that map, and you're done:

const data = [{"id": 1,"movie_title": "Extraction","imdb": 6.7,"length": "1h 56min","movie_image": "https://i.imgur.com/JljBVho.jpg","director": "Sam Hargrave","stars": "Chris Hemsworth, Bryon Lerum, Ryder Lerum","total_season": null,"production_year": 2020,"statu_title": "movie","genre_title": "action"},{"id": 1,"movie_title": "Extraction","imdb": 6.7,"length": "1h 56min","movie_image": "https://i.imgur.com/JljBVho.jpg","director": "Sam Hargrave","stars": "Chris Hemsworth, Bryon Lerum, Ryder Lerum","total_season": null,"production_year": 2020,"statu_title": "movie","genre_title": "thriller"},{"id": 2,"movie_title": "The Old Guard","imdb": 6.7,"length": "2h 5min","movie_image": "https://i.imgur.com/PSYB2RK.jpg","director": "Gina Prince-Bythewood","stars": "Charlize Theron, KiKi Layne, Matthias Schoenaerts","total_season": null,"production_year": 2020,"statu_title": "movie","genre_title": "action"},{"id": 2,"movie_title": "The Old Guard","imdb": 6.7,"length": "2h 5min","movie_image": "https://i.imgur.com/PSYB2RK.jpg","director": "Gina Prince-Bythewood","stars": "Charlize Theron, KiKi Layne, Matthias Schoenaerts","total_season": null,"production_year": 2020,"statu_title": "movie","genre_title": "adventure"},{"id": 2,"movie_title": "The Old Guard","imdb": 6.7,"length": "2h 5min","movie_image": "https://i.imgur.com/PSYB2RK.jpg","director": "Gina Prince-Bythewood","stars": "Charlize Theron, KiKi Layne, Matthias Schoenaerts","total_season": null,"production_year": 2020,"statu_title": "movie","genre_title": "fantasy"}];

let map = new Map(data.map(movie => [movie.id, { ...movie, ...{ genre_title: [] }}]));
for (let {id, genre_title} of data) map.get(id).genre_title.push(genre_title);
let result = Array.from(map.values());

console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286