合并具有相同ID的多个对象-Lodash

时间:2020-09-27 08:59:24

标签: javascript lodash

我已经对此进行了研究,到目前为止,我已经尝试过这些方法:

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

但是我无法成功合并。

这是我的数据:

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"
  }
]

我想做的是:

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"]
  }
]

所以基本上我想通过id合并对象,并将genre_title转换为数组。 实际上,即使我研究了减少和过滤方法,我也无法理解。 (也许我什至不需要lodash ...) 谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用一个具有新对象结构的对象(带有空的genre_title数组)创建一个以id为键的Map。然后,您将迭代数据以填充该新数组。最后,从该地图中提取值,然后完成:

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);

相关问题