![](/img/trans.png)
[英]Can't get JSON data from array inside of array using for loop in javascript/jquery
[英]Can't get data from JavaScript array
我有這個代碼
var obj = [];
$('#next').click(function(){
jQuery.getJSON(produk1 , function(product1) {
hargana1 = product1.price;
obj.push({
harga: hargana1
});
}
jQuery.getJSON(produk2 , function(product1) {
hargana2 = product2.price;
obj.push({
harga: hargana2
});
}
console.log(harga)
});
我的控制台上有這樣的結果
我如何從 harga 獲得價值? 我嘗試使用obj['harga']
它顯示未定義
好吧,如果您仔細觀察,您會發現這實際上是一個充滿對象的數組。 您可以通過方括號[]
看到它是一個數組
像這樣嘗試: obj[0].harga
您應該遍歷數組:
const out = [{harga:21132424},{harga:543535}] console.log(out) out.forEach(obj=>{ const harga = obj.harga; //do something to harga here console.log(harga) })
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.