14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

underscoreのeachではbreakが出来ない

Last updated at Posted at 2013-05-25
var res = 0;
var nums = [1,2,3,4,5,6,7,8,9];

$.each(nums, function(i, v) {
  res = v;
  if (res === 5) {
    return false;
  }

  return true;
});

console.assert(res === 5);

_.each(nums, function(v, i) {
  res = v;
  if (res === 5) {
    return false;
  }

  return true;
});

console.assert(res === 9);

// _.someでbreak
_.some(nums, function(v, i) {
  res = v;
  if (res === 5) {
    return true;
  }

  return false;
});

console.assert(res === 5);

// _.everyでbreak
_.every(nums, function(v, i) {
  res = v;
  if (res === 5) {
    return false;
  }

  return true;
});

console.assert(res === 5);
14
14
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
14

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?