1

so I've retrieved an object containing photos from facebook, Which all works perfectly fine.

for (var i = 0; i < photoLikes.data.length; i++) {

            if (photoLikes.data[i].name === facebook.username) {
                alert("You have already liked this photo. Unliked");
                var count = photoLikes.data.length;
                //facebook.unlikePhoto(id, count);
            }

^^ this code works above,

BUT the issue is when seeing if the name ISNT in the array, what it does is it iterates over every item and if it isnt equal to the username (searching if username is in the object) calls the else statement.

I just need it to be called once. anyone have any idea? this is my statement for not in. I've tried this:

for (var x = 0; x < photoLikes.data.length; x++) {

    if (facebook.photos.indexOf(facebook.username == -1)) {

        console.log("increment");
    }
}

and

for (var x = 0; x < photoLikes.data.length; x++) {

    if (photoLikes.data[i].name != facebook.username) {

        console.log("increment");
    }
}

and both trigger every time.

Thanks

edit: I'm trying to break out of the for loop once the if condition is met.

AlexP2014
  • 211
  • 1
  • 6
  • 15

4 Answers4

5

Basically the (very good) answer of user1680977, but improved with some design patterns (single var, optimized for loop, type safe comparison):

var found = false,
    i, count;

for (i = 0, count = photoLikes.data.length; i < count; i++) {
    if (photoLikes.data[i].name === facebook.username) {
        found = true;
        break;
    }
}

if (found === false) {
    alert("The name was not found in the array");
}

Don´t use "else" in the loop if you don´t need it, keep code in loops as small and fast as possible.

indexOf will not work, because the name is not the key. There is a nice filter method, but not worth it in that case. You may also check out the answer in this thread for more solutions: How do I check if an array includes an object in JavaScript?

Btw, this would be perfect as function, no break needed:

function isUserInArray(likeArray, username) {
    var i, count;

    for (i = 0, count = likeArray.length; i < count; i++) {
        if (likeArray[i].name === username) {
            return true;
        }
    }
    return false;
}
Community
  • 1
  • 1
andyrandy
  • 72,880
  • 8
  • 113
  • 130
1

I'm not exactly sure I know what you're trying to accomplish.

If you're attempting to break out of the for-loop you can simply use the break command. If you are trying to execute a piece of code once in all of the iterations of your for loop you could do something like this.

var executedElse = false;
for( ...... ){
    if( some-condition ){
        // do work
    } else {
        if( !executedElse ){
            // do work
            executedElse = true;
        }
    }
}
captain_jim1
  • 1,004
  • 1
  • 9
  • 18
1

Perhaps try it like this

var found = false;
for (var i = 0; i < photoLikes.data.length; i++) {

    if (photoLikes.data[i].name == facebook.username) {
        found = true;
        break;
    }
}

if (found === false) {
    alert("The name was not found in the array");
}
leopik
  • 2,323
  • 2
  • 17
  • 29
1

Use the break statement:

for (var x = 0; x < photoLikes.data.length; x++) {

    if (facebook.photos.indexOf(facebook.username == -1)) {

        console.log("increment");
        break;
    }
}

MDN docs

Also your if condition is wrong. It should be:

if (facebook.photos.indexOf(facebook.username) == -1) {
    ...
}
Catalin MUNTEANU
  • 5,618
  • 2
  • 35
  • 43