A
A
Andrey2015-01-18 05:27:05
JavaScript
Andrey, 2015-01-18 05:27:05

How to iterate over an array without a double loop?

In short, the goal is to choose cards (Instagram) in which there is my like.
Now I do it like this. Can anyone suggest a more elegant way? jQuery is present

var photos= data.data;
                for (var i= 0; i<options.count; i++) {
                    var photo_card=photos[i]; //Карточка фоты
                    for (var k=0; k<photo_card.likes.data.length; k++)
                        if (photo_card.likes.data[k].username == options.myUsername)
                            $this.append("<li><img src='"+photo_card.images.low_resolution.url+"'></li>")
                }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SagePtr, 2015-01-18
@SagePtr

A more elegant way is to use array functions such as forEach and filter.
Inside, you get the same square in complexity, but the code will be more readable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question