简体   繁体   English

javascript数组比较多次循环

[英]javascript array comparison looping multiple times

so I've retrieved an object containing photos from facebook, Which all works perfectly fine. 因此,我从Facebook检索了一个包含照片的对象,该对象工作正常。

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. 但是问题在于,当查看数组中的名称ISNT时 ,它会遍历每个项目,如果它不等于用户名(搜索用户名是否在对象中),则调用else语句。

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. 编辑:一旦满足条件,我试图打破for循环。

Basically the (very good) answer of user1680977, but improved with some design patterns (single var, optimized for loop, type safe comparison): 基本上是user1680977的(非常好)答案,但是在某些设计模式(单个var,针对循环进行了优化,类型安全的比较)中得到了改进:

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. 如果不需要,请不要在循环中使用“ else”,请使循环中的代码尽可能小和快速。

indexOf will not work, because the name is not the key. indexOf将不起作用,因为名称不是关键字。 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? 您还可以在该线程中查看答案以获取更多解决方案: 如何检查数组是否在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;
}

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. 如果您尝试突破for循环,则可以简单地使用break命令。 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. 如果您试图在for循环的所有迭代中一次执行一段代码,则可以执行以下操作。

var executedElse = false;
for( ...... ){
    if( some-condition ){
        // do work
    } else {
        if( !executedElse ){
            // do work
            executedElse = true;
        }
    }
}

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

Use the break statement: 使用break语句:

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

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

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

MDN docs MDN文档

Also your if condition is wrong. 另外, if条件错误。 It should be: 它应该是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM