-1

So I have two Object Arrays that I need to make one Object Array.

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),
    }];

and if there is a image to go with the episode I have

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

however that does not work. instead it added prints out like

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

Instead it should look like

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

You can see the image part is joined into the first array.

  • `concat()` works on the arrays, it doesn't care about the elements. See `console.log([1,2].concat([3,4]))` instead of that eye-popping monstrosity. – tevemadar Sep 19 '22 at 10:27

3 Answers3

0

You can use this:

if (image) {
    var image = [{"image":"this "}]
    const merged = {...insertEpisode, ...image};
}
Zeevi Gev
  • 33
  • 4
  • 1
    I was able to make this work with some minor changes: `if(im){ const image =[{"image":filesToEpisode[1].image.enclosureurl}]; const merged = {...insertEpisode[0], ...image[0]};}` – RUSSELL HARROWER Sep 22 '22 at 07:59
0
const array3 = insertEpisode.map((item, index) => {
  item = Object.assign({}, item, image[index]);
});
XuWang
  • 179
  • 7
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – CherryDT Sep 19 '22 at 10:21
  • I would like to know how you get the image[index] part? Do I create a new array with the image. – RUSSELL HARROWER Sep 22 '22 at 07:37
  • Tried your code it did not work: let im = filesToEpisode[1].hasOwnProperty("image"); console.log("CHECKED "+ im); if(im){ const image =[{"image":filesToEpisode[1].image.enclosureurl}]; // const array3 = insertEpisode.concat([{"image":"this "}]); const array3 = insertEpisode.map((item, index) => { item = Object.assign({}, item, image[index]); }); console.log(JSON.stringify(array3)); } – RUSSELL HARROWER Sep 22 '22 at 07:55
-1

If you need to add an existing image url to your object, can you just assign a new property to the object of your insertEpisode array?

if (image) {
  insertEpisode[0].image = 'this';
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Gogamar
  • 115
  • 4