Javascript连接两个对象数组



所以我有两个对象数组,我需要制作一个对象数组。

var insertEpisode = [{
"show_id":new ObjectId(req.body.show_id),
"show_b":req.body.show_b,
"season":new Double(req.body.season),
"episode_no": new Double(req.body.episode_no),
"title": req.body.title,
"summary": podsummary,
"description": description,
"explicit":req.body.explicit,
"enclosureurl":filesToEpisode[0].file.enclosureurl,
"enclosurebyts": filesToEpisode[0].file.enclosurebyts,
"duration":new Double(filesToEpisode[0].file.duration),
}];

如果这一集有一张图片,我有

if(image){
var image = [{"image":"this "}]
const array3 = insertEpisode.concat(image);
}

然而,这并不奏效。相反,它添加了类似的打印

[{"show_id":"6274a3a881cf417136a8a4ed","show_b":"insiderelationships","season":1,"episode_no":40,"title":"Test Title","summary":"A week ago a friend invited a couple of other couples over for dinner. Eventually, the food (but not the wine) was cleared off the table for what turned out to be some fierce Scrabbling.","description":"<![CDATA[<p>A week ago a friend invited aco</p>]]>","explicit":"false","enclosureurl":"URL TO mp3.mp3","enclosurebyts":"173080","duration":00:01:00},{"image":"this "}]

相反,它应该看起来像

[{"show_id":"6274a3a881cf417136a8a4ed","show_b":"insiderelationships","season":1,"episode_no":40,"title":"Test Title","summary":"A week ago a friend invited a couple of other couples over for dinner. Eventually, the food (but not the wine) was cleared off the table for what turned out to be some fierce Scrabbling.","description":"<![CDATA[<p>A week ago a friend invited aco</p>]]>","explicit":"false","enclosureurl":"URL TO mp3.mp3","enclosurebyts":"173080","duration":00:01:00, "image":"this "}]

您可以看到图像部分已连接到第一个数组中。

您可以使用这个:

if (image) {
var image = [{"image":"this "}]
const merged = {...insertEpisode, ...image};
}
const array3 = insertEpisode.map((item, index) => {
item = Object.assign({}, item, image[index]);
});

如果您需要将现有的图像url添加到对象中,您可以为insertEpisode数组的对象分配一个新属性吗?

if (image) {
insertEpisode[0].image = 'this';
}

最新更新