循环遍历循环并在满足条件时中断

时间:2021-02-23 10:23:50

I am using jQuery to loop through an array and comparing each value with a model's user id. I will only display a specific text if a match is found.

我使用jQuery循环遍历数组并将每个值与模型的用户ID进行比较。如果找到匹配项,我只会显示特定文本。

$.each current_user.get('following_ids'), (i, e) =>
  console.log(@model.get('user')._id == e)
  if @model.get('user')._id == e
    @is_following = true
    //break from loop if condition is met
    return false
  else
    @is_following = false
    //else continue looping through
    return true

if @is_following
  $(@el).find('.user_info .follow a').text "following"
else
  $(@el).find('.user_info .follow a').text "follow"

However my code is not working, it always returns me "follow" text. What am I doing wrong here?

但是我的代码不起作用,它总是返回“跟随”文本。我在这做错了什么?

2 个解决方案

#1


1  

Presumably current_user.get('following_ids') is some sort of array of IDs. Two possibilities immediately come to mind:

推测current_user.get('following_ids')是某种ID数组。立即想到两种可能性:

  1. current_user.get('following_ids') doesn't contain @model.get('user')._id so everything is working as expected.
  2. current_user.get('following_ids')不包含@ model.get('user')._ id,所以一切都按预期工作。

  3. You have a type problem. Perhaps the following_ids is an array of strings and _id is a number or vice versa.
  4. 你有类型问题。也许following_ids是一个字符串数组,_id是一个数字,反之亦然。

Option 2 needs a little more explanation: CoffeeScript's == is converted to JavaScript's === so 1 == '1' is false in CoffeeScript but true in JavaScript. This does make 2 a hidden and possibly surprising variant of 1 but it is special enough to be its own case.

选项2需要更多解释:CoffeeScript的==转换为JavaScript的===所以1 =='1'在CoffeeScript中是假的,但在JavaScript中是真的。这确实使2成为1的隐藏且可能令人惊讶的变体,但它特别足以成为它自己的情况。

Consider this simplified analogue of your situation:

考虑一下这种情况的简化模拟:

$.each ['1','2','3'], (i, e) =>
    if 2 == e
        @is_following = true
        return false
    else
        @is_following = false
        return true
console.log @is_following

You'll get false out of that because 2 == '2' is false is CoffeeScript: http://jsfiddle.net/ambiguous/YsstH/

你会得到错误,因为2 =='2'是假的是CoffeeScript:http://jsfiddle.net/ambiguous/YsstH/

But, if we fix the types:

但是,如果我们修复类型:

$.each [1,2,3], (i, e) =>
    # Only the array changes...
console.log @is_following

Then we get the true result that we're expecting: http://jsfiddle.net/ambiguous/CxHXu/

然后我们得到了我们期待的真实结果:http://jsfiddle.net/ambiguous/CxHXu/


In any case, since you're using Backbone, you have Underscore so you could just use _.any:

在任何情况下,由于您使用的是Backbone,因此您可以使用Underscore,因此您可以使用_.any:

@is_following = _(current_user.get('following_ids')).any (id) => @model.get('user')._id == id

or better:

want_this_id  = @model.get('user')._id
@is_following = _(current_user.get('following_ids')).any (id) -> want_this_id == id

You'd still have to sort out the type problem though.

你仍然需要解决类型问题。

#2


0  

Most likely, you're overwriting the result after you've found the match. If you've found a match, there's no need to keep checking.

最有可能的是,在找到匹配项后,您将覆盖结果。如果您找到匹配项,则无需继续检查。

$.each current_user.get('following_ids'), (i, e) =>
  unless @is_following
    console.log(@model.get('user')._id == e)
    if @model.get('user')._id == e
       @is_following = true
      //break from loop if condition is met
      return false
    else
      @is_following = false
      //else continue looping through
      return true

if @is_following
  $(@el).find('.user_info .follow a').text "following"
else
  $(@el).find('.user_info .follow a').text "follow"

#1


1  

Presumably current_user.get('following_ids') is some sort of array of IDs. Two possibilities immediately come to mind:

推测current_user.get('following_ids')是某种ID数组。立即想到两种可能性:

  1. current_user.get('following_ids') doesn't contain @model.get('user')._id so everything is working as expected.
  2. current_user.get('following_ids')不包含@ model.get('user')._ id,所以一切都按预期工作。

  3. You have a type problem. Perhaps the following_ids is an array of strings and _id is a number or vice versa.
  4. 你有类型问题。也许following_ids是一个字符串数组,_id是一个数字,反之亦然。

Option 2 needs a little more explanation: CoffeeScript's == is converted to JavaScript's === so 1 == '1' is false in CoffeeScript but true in JavaScript. This does make 2 a hidden and possibly surprising variant of 1 but it is special enough to be its own case.

选项2需要更多解释:CoffeeScript的==转换为JavaScript的===所以1 =='1'在CoffeeScript中是假的,但在JavaScript中是真的。这确实使2成为1的隐藏且可能令人惊讶的变体,但它特别足以成为它自己的情况。

Consider this simplified analogue of your situation:

考虑一下这种情况的简化模拟:

$.each ['1','2','3'], (i, e) =>
    if 2 == e
        @is_following = true
        return false
    else
        @is_following = false
        return true
console.log @is_following

You'll get false out of that because 2 == '2' is false is CoffeeScript: http://jsfiddle.net/ambiguous/YsstH/

你会得到错误,因为2 =='2'是假的是CoffeeScript:http://jsfiddle.net/ambiguous/YsstH/

But, if we fix the types:

但是,如果我们修复类型:

$.each [1,2,3], (i, e) =>
    # Only the array changes...
console.log @is_following

Then we get the true result that we're expecting: http://jsfiddle.net/ambiguous/CxHXu/

然后我们得到了我们期待的真实结果:http://jsfiddle.net/ambiguous/CxHXu/


In any case, since you're using Backbone, you have Underscore so you could just use _.any:

在任何情况下,由于您使用的是Backbone,因此您可以使用Underscore,因此您可以使用_.any:

@is_following = _(current_user.get('following_ids')).any (id) => @model.get('user')._id == id

or better:

want_this_id  = @model.get('user')._id
@is_following = _(current_user.get('following_ids')).any (id) -> want_this_id == id

You'd still have to sort out the type problem though.

你仍然需要解决类型问题。

#2


0  

Most likely, you're overwriting the result after you've found the match. If you've found a match, there's no need to keep checking.

最有可能的是,在找到匹配项后,您将覆盖结果。如果您找到匹配项,则无需继续检查。

$.each current_user.get('following_ids'), (i, e) =>
  unless @is_following
    console.log(@model.get('user')._id == e)
    if @model.get('user')._id == e
       @is_following = true
      //break from loop if condition is met
      return false
    else
      @is_following = false
      //else continue looping through
      return true

if @is_following
  $(@el).find('.user_info .follow a').text "following"
else
  $(@el).find('.user_info .follow a').text "follow"