[英]Merge objects based on id and its properties in an arrays
I am trying to merge two multi level objects with in an array using same id.我正在尝试使用相同的 ID 将两个多级对象合并到一个数组中。 Please help.请帮忙。
The input array of objects looks like this输入的对象数组如下所示
{
"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",
"status": "Yes",
"images":[{
"id": 1,
"fileType": "image",
"fileName": "movie1.0.jpg",
"filePath": "images\\movie1.0.jpg",
"fileExten": ".jpg"
},
{
"id": 2,
"fileType": "image",
"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,
"fileType": "video",
"fileName": "movie1.1.mp4",
"filePath": "videos\\movie1.1.mp4",
"fileExten": ".mp4"
}]
}]
},
{
"id": 1,
"movieId": 3,
"user": "test@test.com",
"title": "test Project",
"desc": "test 123 test 123test 123test 123test 123",
"status": "Yes",
"movies":[{
"id": 3,
"title": "Movie 3",
"desc": "Movie 3 Movie 3 Movie 3 Movie 3",
"status": "Yes",
"images":[{
"id": 8,
"fileType": "image",
"fileName": "movie2.0.jpg",
"filePath": "images\\movie2.0.jpg",
"fileExten": ".jpg"
}],
"videos":[{
"id": 12,
"fileType": "video",
"fileName": "movie2.1.mp4",
"filePath": "videos\\movie2.1.mp4",
"fileExten": ".mp4"
},
{
"id": 16,
"fileType": "video",
"fileName": "movie2.2.mp4",
"filePath": "videos\\movie2.2.mp4",
"fileExten": ".mp4"
},
{
"id": 20,
"fileType": "video",
"fileName": "movie2.3.mp4",
"filePath": "videos\\movie2.3.mp4",
"fileExten": ".mp4"
}]
}]
},{
"id": 2,
"user": "test@test.com",
"title": "Test Project 2",
"desc": "project2 123 project2 123 project2 123 project2 123 project2 123 ",
"status": "Yes",
"movies":[]
}
]
}
The above output is produced by below code.上面的 output 是由下面的代码生成的。
I need to merge two object which has same Id so that the inner movies arrays also get append / merged.我需要合并两个具有相同 ID 的 object,以便内部电影 arrays 也得到 append / 合并。 I have tried many ways but i couldn't get the desire output. Please help.我尝试了很多方法,但我无法得到想要的 output。请帮助。
let groupList = [];
await Promise.all(
await getGroups(user).map(async group => {
group.movies = [await getMovies(group.movieId)];
groupList.push(group);
return groupList;
}));
I found the solution by myself我自己找到了解决方案
Solution:解决方案:
------------- --------------
The below code returns the exact desired output which is expected.下面的代码返回预期的确切所需的 output。
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);
Desire output:欲望 output:
{
"data": [
{
"id": 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",
"status": "Yes",
"images":[{
"id": 1,
"fileType": "image",
"fileName": "movie1.0.jpg",
"filePath": "images\\movie1.0.jpg",
"fileExten": ".jpg"
},
{
"id": 2,
"fileType": "image",
"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,
"fileType": "video",
"fileName": "movie1.1.mp4",
"filePath": "videos\\movie1.1.mp4",
"fileExten": ".mp4"
}]
},{
"id": 3,
"title": "Movie 3",
"desc": "Movie 3 Movie 3 Movie 3 Movie 3",
"status": "Yes",
"images":[{
"id": 8,
"fileType": "image",
"fileName": "movie2.0.jpg",
"filePath": "images\\movie2.0.jpg",
"fileExten": ".jpg"
}],
"videos":[{
"id": 12,
"fileType": "video",
"fileName": "movie2.1.mp4",
"filePath": "videos\\movie2.1.mp4",
"fileExten": ".mp4"
},
{
"id": 16,
"fileType": "video",
"fileName": "movie2.2.mp4",
"filePath": "videos\\movie2.2.mp4",
"fileExten": ".mp4"
},
{
"id": 20,
"fileType": "video",
"fileName": "movie2.3.mp4",
"filePath": "videos\\movie2.3.mp4",
"fileExten": ".mp4"
}]
}]
},
{
"id": 2,
"user": "test@test.com",
"title": "Test Project 2",
"desc": "project2 123 project2 123 project2 123 project2 123 project2 123 ",
"status": "Yes",
"movies":[]
}
]
}
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", "status": "Yes", "images":[{ "id": 1, "fileType": "image", "fileName": "movie1.0.jpg", "filePath": "images\\movie1.0.jpg", "fileExten": ".jpg" }, { "id": 2, "fileType": "image", "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, "fileType": "video", "fileName": "movie1.1.mp4", "filePath": "videos\\movie1.1.mp4", "fileExten": ".mp4" }] }] }, { "id": 1, "movieId": 3, "user": "test@test.com", "title": "test Project", "desc": "test 123 test 123test 123test 123test 123", "status": "Yes", "movies":[{ "id": 3, "title": "Movie 3", "desc": "Movie 3 Movie 3 Movie 3 Movie 3", "status": "Yes", "images":[{ "id": 8, "fileType": "image", "fileName": "movie2.0.jpg", "filePath": "images\\movie2.0.jpg", "fileExten": ".jpg" }], "videos":[{ "id": 12, "fileType": "video", "fileName": "movie2.1.mp4", "filePath": "videos\\movie2.1.mp4", "fileExten": ".mp4" }, { "id": 16, "fileType": "video", "fileName": "movie2.2.mp4", "filePath": "videos\\movie2.2.mp4", "fileExten": ".mp4" }, { "id": 20, "fileType": "video", "fileName": "movie2.3.mp4", "filePath": "videos\\movie2.3.mp4", "fileExten": ".mp4" }] }] },{ "id": 2, "user": "test@test.com", "title": "Test Project 2", "desc": "project2 123 project2 123 project2 123 project2 123 project2 123 ", "status": "Yes", "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]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.