0

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?

1 Answer 1

1

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)
    }
  })
})

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.