Yamil Vernet Yamil Vernet - 6 months ago 44
Javascript Question

javascript print multidimensional array

i have an error when i try to get values of an array to append in a div HTML

the code Jquery is this:

var products = [
{ title: 'Botín', price: 800.44, category: 'Botines', image: '../images/005030000D65804_1.jpg', brand: 'nike' },
{ title: 'Ojotas', price: 300.99, category: 'Ojotas', image: '../images/004632000F88356_1.jpg', brand: 'adidas' },
{ title: 'Zapatillas', price: 1120.00, category: 'Calzado', image: '../077632604336703_1.jpg', brand: 'puma' },
{ title: 'Short', price: 320.44, category: 'Vestimenta', image: '../images/077652619598012_1.jpg', brand: 'nike' },
{ title: 'Pantalón', price: 360.44, category: 'Natación', image: '../images/005030000D65804_9.jpg', brand: 'nike' }
];


i use for to get the values and it gives me "undefined" i dont understand.

i tried to post the code i used, but i did some changes and now i dont remember it. Pleaaase help!

Answer

As stated in the comments, products is an array of objects. So you'd have to iterate through them as such:

jQuery(products).each(function(i, obj) {
    jQuery('div#your_div_id').append(obj.title + ' costs $' + obj.price);
    // etc etc...
});

jsFiddle Example

Comments