Skip to content

Instantly share code, notes, and snippets.

@daboross
Created January 8, 2017 08:41
Show Gist options
  • Save daboross/42e47fed3da51b931f44b22ef441c042 to your computer and use it in GitHub Desktop.
Save daboross/42e47fed3da51b931f44b22ef441c042 to your computer and use it in GitHub Desktop.
Strange behavior with _.each
$ node test.js
lodash version: 4.17.4
_.each: false
_.each w/ brackets: true
built-in forEach: true
_ = require('lodash');
console.log('lodash version: ' + _.VERSION);
function createBlankArray() {
return _.times(3, () => _.times(3, () => false));
}
function testArray(name) {
console.log(name + ': ' + _.some(array2d, x => _.some(x)));
}
let values = [
{x: 0, y: 0, value: 'a'},
{x: 1, y: 0, value: 'a'},
{x: 2, y: 0, value: 'b'},
{x: 0, y: 1, value: 'a'},
{x: 1, y: 1, value: 'b'},
{x: 2, y: 1, value: 'b'},
{x: 0, y: 2, value: 'b'},
{x: 1, y: 2, value: 'b'},
{x: 2, y: 2, value: 'a'},
]
let array2d = createBlankArray();
_.each(values, obj => array2d[obj.x][obj.y] = obj.value == 'b');
testArray('_.each', array2d);
array2d = _.times(3, () => _.times(3, () => false));
_.each(values, obj => {array2d[obj.x][obj.y] = obj.value == 'b'});
testArray('_.each w/ brackets', array2d);
array2d = _.times(3, () => _.times(3, () => false));
values.forEach(obj => array2d[obj.x][obj.y] = obj.value == 'b');
testArray('built-in forEach', array2d);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment