Javascript 如何从JSON数组中获取特定数据?
我从数组中获取JSON数据时遇到问题 我尝试打印阵列数据,但它不起作用:Javascript 如何从JSON数组中获取特定数据?,javascript,json,Javascript,Json,我从数组中获取JSON数据时遇到问题 我尝试打印阵列数据,但它不起作用: <div id='asd'>lalala</div> <button onclick='myfunction()'>click me</button> <script> function myfunction(){ var arr = []; fetch('https://api.blockchair.com/bitcoi
<div id='asd'>lalala</div>
<button onclick='myfunction()'>click me</button>
<script>
function myfunction(){
var arr = [];
fetch('https://api.blockchair.com/bitcoin-sv/blocks?a=month,median(transaction_count)')
.then(response => response.json())
.then(result => arr.push(result));
console.log(arr)
document.getElementById('asd').innerHTML = arr[0]
}
</script>
lalala
点击我
函数myfunction(){
var-arr=[];
取('https://api.blockchair.com/bitcoin-sv/blocks?a=month,中位数(交易计数)
.then(response=>response.json())
。然后(结果=>arr.push(结果));
控制台日志(arr)
document.getElementById('asd')。innerHTML=arr[0]
}
我得到“未定义错误”。您的问题是:
console.log(arr)
document.getElementById('asd').innerHTML=arr[0];
超出最终的。然后
,这意味着它将在代码首次执行时运行,而不是在方法收到结果后运行。相反,您需要将其放入最后的。然后
回调中,以便它首先填充数组,然后输出结果:
function myfunction(){
var arr = [];
fetch('https://api.blockchair.com/bitcoin-sv/blocks?a=month,median(transaction_count)')
.then(response => response.json())
.then(result => {
arr.push(result);
console.log(arr);
document.getElementById('asd').innerHTML = JSON.stringify(arr[0]);
});
}
此外,如果要将输出添加到DOM,则需要将其字符串化,以便正确显示数据。您的问题在于:
console.log(arr)
document.getElementById('asd').innerHTML=arr[0];
超出最终的。然后
,这意味着它将在代码首次执行时运行,而不是在方法收到结果后运行。相反,您需要将其放入最后的。然后
回调中,以便它首先填充数组,然后输出结果:
function myfunction(){
var arr = [];
fetch('https://api.blockchair.com/bitcoin-sv/blocks?a=month,median(transaction_count)')
.then(response => response.json())
.then(result => {
arr.push(result);
console.log(arr);
document.getElementById('asd').innerHTML = JSON.stringify(arr[0]);
});
}
此外,如果要将输出添加到DOM,则需要将其字符串化,以便正确显示数据。您确实需要修复回调函数,并且数组对象也存在于数据中。请参考下面的代码片段
lalala
点击我
函数myfunction(){
var-arr=[];
取('https://api.blockchair.com/bitcoin-sv/blocks?a=month,中位数(交易计数)
.然后(功能(响应){
返回response.json();
}).then(函数(myJson){
arr.push(JSON.stringify(myJson.data));
document.getElementById('asd')。innerHTML=arr[0];
});
}
您确实需要修复您的回调函数以及数据中存在的数组对象。请参考下面的代码片段
lalala
点击我
函数myfunction(){
var-arr=[];
取('https://api.blockchair.com/bitcoin-sv/blocks?a=month,中位数(交易计数)
.然后(功能(响应){
返回response.json();
}).then(函数(myJson){
arr.push(JSON.stringify(myJson.data));
document.getElementById('asd')。innerHTML=arr[0];
});
}
代码中的问题是,您正在使用fetch联系服务器,这是一个异步操作,因为您需要在then
函数中等待服务器的响应
您需要在函数中记录数组,然后在函数中记录数组
因此,它应该是:
function myfunction(){
var arr = [];
fetch('https://api.blockchair.com/bitcoin-sv/blocks?a=month,median(transaction_count)')
.then(response => response.json())
.then(result => {
arr.push(result)
console.log(arr)
document.getElementById('asd').innerHTML=arr[0]
});
}
代码中的问题是,您正在使用fetch联系服务器,这是一个异步操作,因为您需要在then
函数中等待服务器的响应
您需要在函数中记录数组,然后在函数中记录数组
因此,它应该是:
function myfunction(){
var arr = [];
fetch('https://api.blockchair.com/bitcoin-sv/blocks?a=month,median(transaction_count)')
.then(response => response.json())
.then(result => {
arr.push(result)
console.log(arr)
document.getElementById('asd').innerHTML=arr[0]
});
}
这就是你想要的
函数myfunction(){
取('https://api.blockchair.com/bitcoin-sv/blocks?a=month,中位数(交易计数)
.then(response=>response.json())
.then(theJson=>{
const arr=theJson.data
document.getElementById('asd').innerHTML=JSON.stringify(arr[0])
})
}
lalala
单击我这是您想要的
函数myfunction(){
取('https://api.blockchair.com/bitcoin-sv/blocks?a=month,中位数(交易计数)
.then(response=>response.json())
.then(theJson=>{
const arr=theJson.data
document.getElementById('asd').innerHTML=JSON.stringify(arr[0])
})
}
lalala
单击我
post your json data here我们需要您的json来解决您的问题。post your json data here我们需要您的json来解决您的问题。