JavaScript
jQuery
Underscore.js

underscoreのeachではbreakが出来ない

More than 5 years have passed since last update.

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