Solution 1 :

Import the file with an alias. 🙂

import * as file from './file.json'

Solution 2 :

your second JSON looks like an array of objects. you should be able to get title using array[index][‘title’]. if this is not what you’re looking to do, please further elaborate.

Problem :

I have two Javascript/JSON Files. In my First JScript File I am getting the Elements (title, src, etc.) and put them into my HTML file, when I click a Button. But I don’t really know how to get access to the title or src tag from the second JSON source File.

// First Java File

this.addNewElementAction.onclick = () => {
    const new Item = {
      title: "some title",
      src: "some source"
    }

    addNewElementToList(obj) {
      const li = document.createElement("li");
      const img = document.createElement("img");
      li.appendChild(img);
      img.classList.add("align-left");
      img.src = "./data/img/" + obj.src;
    }
// How my second JSON File looks like

[ { "title": "xyz", "src": "https://www...." } ]

By

Leave a Reply

Your email address will not be published. Required fields are marked *