1

I built the following for Loop and wanted to ask if there is a way to simplify it:

//data contains an array
var i = 0;
if(data.length >= 1) {
    i = 1;
}
for (i; i <= data.length; i++) {
    $('#img-thumb-id' + i).attr('src', data[i-1]);
}
3
  • You can use $.each. And always start loop index from zero. Commented Jun 19, 2017 at 11:15
  • 1
    Do i understand right that you want it to be displayed once if data.length == 0? It doesn't appear to work in that case .. Commented Jun 19, 2017 at 11:16
  • @Adder i think that was an attempt to also check if data is empty Commented Jun 19, 2017 at 11:22

5 Answers 5

3

I see that jQuery is used here, so you can use $.each

$.each(data, function(i, item){

   var iteration = +i+1;
   $('#img-thumb-id' + iteration).attr('src', item);

});

if you want to use vanilla JavaScript (no jQuery at all)

for (var i; i < data.length; i++) {
   var iteration = +i+1;
   document.getElementById('img-thumb-id'+iteration).src = data[i];
}

Or you can use:

for (var i in data) {
   document.getElementById('img-thumb-id'+(+i+1)).src = data[i];
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you want to offer a vanilla solution might I suggest you replace $('#img-thumb-id' + iteration) with document.getElementById('img-thumb-id'+iteration) I know the OP is using a jQuery selector but for future visitors it could be misleading or confusing as they will be expecting vanilla only.
1
if(typeof data!=="undefined" && data && data.length>0) {
    for (var i=0; i < data.length; i++) {
        $('#img-thumb-id' + (i+1)).attr('src', data[i]);
    }
}

11 Comments

if(data && data.length) is equal to your condition
@Jonasw please check this example, it is not equal jsfiddle.net/ftr46syd/1
@Jonasw if(typeof data!=="undefined" && data && data.length) and typeof data!=="undefined" && data && data.length>0 perform better.
@Jonasw yes it perform better check the fiddle, do not define data and check the output, do the same for data being null,"",[],{} you will see some differences, to sum up this is more fail safe typeof data!=="undefined" && data && data.length i think if you really had the time to check the fiddle you would agree on that. Best regards.
@Jonasw and in addition in case you would like to mimic PHP's empty function what would you think of something like this jsfiddle.net/ftr46syd/2 ?
|
1

The loop is fine, but I don't see why you don't just do:

if(data !== null){
    for (var i = 0; i < data.length; i++) {
                $('#img-thumb-id' + (i + 1)).attr('src', data[i]);
            }
}

You don't need to do the first block of code in your question. It is more clear to people reading your code if you just do a standard for-loop starting from 0 and going up to the object.length.

Comments

1

You may also try this :

data.forEach(function(item, index){
   $('#img-thumb-id' + (index+1)).attr('src', item);
});

2 Comments

be careful with '#img-thumb-id' + index+1 ...won't produce what you expect
Thank you @charlietfl placed index+1 inside parenthesis
-1

Use simple foreach loop in JavaScript

//for eg. data array may be
var data = [1,2,3,4];
data.forEach(function(item, index){
$('#img-thumb-id' + (index+1)).attr('src', item);
});

3 Comments

iteration is undefined.
Thanks, for the undefined the index is used
@downvoter please remove your downvote, tue answer is now valid

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.