Reputation: 25
I'm looping through JSON data but when I display the data I don't think the quality of the code is good enough, I feel like I'm doing something wrong.
$(function(){
$.getJSON('data.json', function(data){
let content = '';
for(let x in data){
content += data[x]
}
$('.account-image').eq(0).attr('src', data[0].logo)
$('.account-image').eq(1).attr('src', data[1].logo)
$('.account-image').eq(2).attr('src', data[2].logo)
$('.account-image').eq(3).attr('src', data[3].logo)
$('.account-image').eq(4).attr('src', data[4].logo)
$('.account-image').eq(5).attr('src', data[5].logo)
$('.account-image').eq(6).attr('src', data[6].logo)
$('.account-image').eq(7).attr('src', data[7].logo)
})
})
Is there a way to loop through the $(.account-image)
like the one written above but dynamically?
Upvotes: 0
Views: 49
Reputation: 195992
How about
$(function() {
$.getJSON('data.json', function(data) {
let content = '';
for (let x in data) {
content += data[x];
$('.account-image').eq(x).attr('src', data[x].logo)
}
})
})
Upvotes: 1