For loop adds backslash and needs separation

2020-03-26 javascript json for-loop

I am creating some structured data with JSON-LD for a website. The website contains images, and for each image I need to output a link to each image.

My output is:

  "image": [
    "https://a.storyblok.com/f/72895/1140x380/c97d9f7287/5dd3fa9c10793dd70ffa7851_press_en_1.pnghttps://article.images.consumerreports.org/f_auto/prod/content/dam/CRO%20Images%202019/Cars/November/CR-Cars-InlineHero-2020-Toyota-Prius-AWD-f-11-19"
  ]

My desired output is:

  "image": [
    "https://a.storyblok.com/f/72895/1140x380/c97d9f7287/5dd3fa9c10793dd70ffa7851_press_en_1.png",
    "https://article.images.consumerreports.org/f_auto/prod/content/dam/CRO%20Images%202019/Cars/November/CR-Cars-InlineHero-2020-Toyota-Prius-AWD-f-11-19"
  ]

My current code is:

    let imageUrls = ''
    let i
    for (i = 0; i < this.stories[0].images.length; i++) {
      imageUrls += this.stories[0].images[i].link
    }

How do I handle this? I have tried adding quotes with this code:

    let imageUrls = ''
    let i
    for (i = 0; i < this.stories[0].images.length; i++) {
      imageUrls += this.stories[0].images[i].link + '",'
    }

The result is, as you can see not good. It also adds a backslash for some reason:

  "image": [
    "https://a.storyblok.com/f/72895/1140x380/c97d9f7287/5dd3fa9c10793dd70ffa7851_press_en_1.png\",https://article.images.consumerreports.org/f_auto/prod/content/dam/CRO%20Images%202019/Cars/November/CR-Cars-InlineHero-2020-Toyota-Prius-AWD-f-11-19\","
  ]

Answers

Related