[英]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
.我正在尝试从我的本地 Json 获取 API。我的 console.log 显示了我在for loop
中真正想要的。 But my output.innerHTML
doesn't work.但是我的output.innerHTML
不起作用。 I tried firstly with my <div></div>
tags and then I convert It to pre
tag.我首先尝试使用<div></div>
标签,然后将其转换为pre
标签。 Still doesn't work.It just shows one line.What I am missing any idea?仍然不起作用。它只显示一行。我想念什么?
data.json:数据.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): index.html(省略硬代码):
<body>
<pre id="output"><br></pre>
<script src="main.js"></script>
</body>
main.js file: main.js 文件:
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"))
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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.