I have this code
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)
});
And I have result on my console like this
How can I get value from harga?
I try with obj['harga'] It shows undefined
2 Answers 2
Well if you take a closer look then you will see that this is actually an array filled with objects. you can see that its an array by the brackets []
Try it like this: obj[0].harga
answered Apr 22, 2020 at 8:19
Ilijanovic
15k4 gold badges29 silver badges63 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Ilijanovic
could you show us some code? with just the data i can not help you
You should iterate through the array :
const out = [{harga:21132424},{harga:543535}]
console.log(out)
out.forEach(obj=>{
const harga = obj.harga;
//do something to harga here
console.log(harga)
})
answered Apr 22, 2020 at 8:25
Nilanka Manoj
3,7474 gold badges21 silver badges52 bronze badges
Comments
lang-js
obj[0].harga.Cannot read property 'harga' of undefinedwhen I try to obj.lenght, it's showing undefined, I think I have weird format of my array