根据ID及其属性在数组中合并对象

问题描述

我正在尝试使用相同的ID将两个多级对象合并到一个数组中。请帮忙。

对象的输入数组如下

{
    "data": [
       {
            "id": 1,"movieId": 1,"user": "test@test.com","title": "test Project","desc": "test 123 test 123test 123test 123test 123","status": "Yes","movies":[{
                "id": 1,"title": "Movie 1","desc": "Movie 1 Movie 1 Movie 1 Movie 1","images":[{
                        "id": 1,"fileType": "image","fileName": "movie1.0.jpg","filePath": "images\\movie1.0.jpg","fileExten": ".jpg"
                    },{
                        "id": 2,"fileName": "movie1.1.jpg","filePath": "images\\movie1.1.jpg","fileExten": ".jpg"
                }],"videos":[{
                        "id": 4,"fileType": "video","fileName": "movie1.0.mp4","filePath": "videos\\movie1.0.mp4","fileExten": ".mp4"
                    },{
                        "id": 5,"fileName": "movie1.1.mp4","filePath": "videos\\movie1.1.mp4","fileExten": ".mp4"
                }]
            }]
        },{
            "id": 1,"movieId": 3,"movies":[{
                "id": 3,"title": "Movie 3","desc": "Movie 3 Movie 3 Movie 3 Movie 3","images":[{
                    "id": 8,"fileName": "movie2.0.jpg","filePath": "images\\movie2.0.jpg","videos":[{
                        "id": 12,"fileName": "movie2.1.mp4","filePath": "videos\\movie2.1.mp4",{
                        "id": 16,"fileName": "movie2.2.mp4","filePath": "videos\\movie2.2.mp4",{
                        "id": 20,"fileName": "movie2.3.mp4","filePath": "videos\\movie2.3.mp4","fileExten": ".mp4"
                }]

            }]
        },{
            "id": 2,"title": "Test Project 2","desc": "project2 123 project2  123 project2  123 project2  123 project2  123 ","movies":[]
        }
    ]
}

上面的输出是由下面的代码产生的。

我需要合并两个具有相同ID的对象,以便内部电影数组也可以追加/合并。我已经尝试了许多方法,但是我无法获得期望的输出。请帮忙。

let groupList = [];
await Promise.all(
    await getGroups(user).map(async group => {
    group.movies = [await getMovies(group.movieId)];
    groupList.push(group);
    return groupList;
  }));

我自己找到了解决方法

解决方案:

-------------

下面的代码返回预期的确切期望输出

await Promise.all(
    await getGroups(user).map(async (item,key) => {
      item.movies = [];
      groupList[key] = item;
      let movies = (item.movies_list) ?  item.movies_list.split(','): []; // it's looks like "1,2" string so converting it to array
      await Promise.all(
        movies.map( async (id,index) => {
            groupList[key].movies.push(await getMovies(id));
          return groupList;
        })
      ) 
    })
  );
console.log(groupList);

期望输出

{
    "data": [
        {
            "id": 1,"fileExten": ".jpg"
                    }],"fileExten": ".mp4"
                    }]

            },{
                "id": 3,"movies":[]
        }
    ]
}

解决方法

使用Map

var moviesData = { "data": [ { "id": 1,"movieId": 1,"user": "test@test.com","title": "test Project","desc": "test 123 test 123test 123test 123test 123","status": "Yes","movies":[{ "id": 1,"title": "Movie 1","desc": "Movie 1 Movie 1 Movie 1 Movie 1","images":[{ "id": 1,"fileType": "image","fileName": "movie1.0.jpg","filePath": "images\\movie1.0.jpg","fileExten": ".jpg" },{ "id": 2,"fileName": "movie1.1.jpg","filePath": "images\\movie1.1.jpg","fileExten": ".jpg" }],"videos":[{ "id": 4,"fileType": "video","fileName": "movie1.0.mp4","filePath": "videos\\movie1.0.mp4","fileExten": ".mp4" },{ "id": 5,"fileName": "movie1.1.mp4","filePath": "videos\\movie1.1.mp4","fileExten": ".mp4" }] }] },{ "id": 1,"movieId": 3,"movies":[{ "id": 3,"title": "Movie 3","desc": "Movie 3 Movie 3 Movie 3 Movie 3","images":[{ "id": 8,"fileName": "movie2.0.jpg","filePath": "images\\movie2.0.jpg","videos":[{ "id": 12,"fileName": "movie2.1.mp4","filePath": "videos\\movie2.1.mp4",{ "id": 16,"fileName": "movie2.2.mp4","filePath": "videos\\movie2.2.mp4",{ "id": 20,"fileName": "movie2.3.mp4","filePath": "videos\\movie2.3.mp4","title": "Test Project 2","desc": "project2 123 project2  123 project2  123 project2  123 project2  123 ","movies":[] } ] }

let makeMap = new Map();
moviesData.data.forEach((o) => {
  if (makeMap.has(o.id)) {
    let oldmov = makeMap.get(o.id).movies;
    o.movies = [...oldmov,...o.movies];
    makeMap.set(o.id,o);
  } else makeMap.set(o.id,o);
});

//console.log(makeMap.entries());
console.log([...makeMap]);