Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

innerHTML and console.log difference in Javascript

I am trying to fetch API from my local Json. My console.log shows what I exactly want in for loop. But my output.innerHTML doesn’t work. I tried firstly with my <div></div> tags and then I convert It to pre tag. Still doesn’t work.It just shows one line.What I am missing any idea ?

data.json :

"resultCode": "0",
    "resultDescription": "success",
    "body": {
        "intraDayTradeHistoryList": [
            {
                "id": 444121195,
                "date": "2022-01-26T00:00:34.000+0300",
                "conract": "PH22012603",
                "price": 731.99,
                "quantity": 5
            },
            {
                "id": 444121022,
                "date": "2022-01-26T00:00:35.000+0300",
                "conract": "PH22012603",
                "price": 732,
                "quantity": 5
            },
            {
                "id": 444121234,
                "date": "2022-01-26T00:00:43.000+0300",
                "conract": "PH22012603",
                "price": 731.99,
                "quantity": 5
            },
            {
                "id": 444120877,
                "date": "2022-01-26T00:00:56.000+0300",
                "conract": "PH22012608",
                "price": 1341.99,
                "quantity": 10
            },
            {
                "id": 444121289,
                "date": "2022-01-26T00:00:56.000+0300",
                "conract": "PH22012608",
                "price": 1341.98,
                "quantity": 10
            },
            {
                "id": 444121519,
                "date": "2022-01-26T00:00:57.000+0300",
                "conract": "PH22012608",
                "price": 1342,
                "quantity": 120
            },
            {
                "id": 444120887,
                "date": "2022-01-26T00:00:58.000+0300",
                "conract": "PH22012608",
                "price": 1342.88,
                "quantity": 50
            },
            {
                "id": 444121529,
                "date": "2022-01-26T00:00:59.000+0300",
                "conract": "PH22012608",
                "price": 1342.9,
                "quantity": 65
            },
            {
                "id": 444120892,
                "date": "2022-01-26T00:01:00.000+0300",
                "conract": "PH22012608",
                "price": 1343.8,
                "quantity": 100
            },
]};

index.html(omitted hard codes) :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<body>
    <pre id="output"><br></pre>
    <script src="main.js"></script>
</body>

main.js file :

const output = document.getElementById("output");

const jsonData = fetch("./data.json")
.then(res => res.json())
.then(data => {
    //const dataLen = data.body.intraDayTradeHistoryList.length;
    const tradeHist = () => 
    {
    for(let i = 0; i <= 5;i++){
          output.innerHTML = (JSON.stringify(data.body.intraDayTradeHistoryList[i]));
          console.log(JSON.stringify(data.body.intraDayTradeHistoryList[i]));

    }
}
tradeHist();
})
.catch(error => console.log("Error"))

>Solution :

As explained in my comment, here’s the fixed code:

const jsonData = fetch("./data.json")
.then(res => res.json())
.then(({body: {intraDayTradeHistoryList}}) => {
    let html = '';
    for (const entry of intraDayTradeHistoryList){
        html += JSON.stringify(entry);
    }
    output.innerHTML = html;
})
.catch(error => console.log(error))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading