0

I'd like to append all the values(without the keys) from this JSON file to an array in a javascript file

JSON File:

[
    {
        "href": "https://www.instagram.com/p/test1/"
    },
    {
        "href": "https://www.instagram.com/p/test2/"
    }
]

JavaScript file:

var ids = [];

const makePost = (id) => `
  <div class="post" id="${id}">
    <blockquote
      class="instagram-media"
      data-instgrm-permalink="https://www.instagram.com/p/${id}/?utm_source=ig_embed&amp;utm_campaign=loading"
      data-instgrm-version="13">
      Content - ${id}
    </blockquote>                
  </div>`;

let htmlContent = ''

ids.forEach(id => {
  htmlContent += makePost(id)
})

var d1 = document.getElementById('dynamic-content');
d1.insertAdjacentHTML('beforeend', htmlContent);
gsx
  • 41
  • 3

1 Answers1

3

You could first use map to get each href value, and the use arra y destructuring to append the values

  let dest = [];
  const array = [{
      "href": "https://www.instagram.com/p/test1/"
    },
    {
      "href": "https://www.instagram.com/p/test2/"
    }
  ]
  
  const values = array.map(entry => entry.href)
  
  dest = [...values]
  
  console.log(dest)
Mario
  • 4,784
  • 3
  • 34
  • 50
  • My JSON file is not inside the javascript file, they're two files('instagram.json' and 'script.js'), how could I do this in this situation? – gsx Jun 30 '21 at 03:24
  • You first need to get the content of `instagram.json` in your script, you could use fetch https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API or probably some of this ideas https://stackoverflow.com/questions/19706046/how-to-read-an-external-local-json-file-in-javascript – Mario Jun 30 '21 at 03:26