diff --git a/lib/handlebars/helpers/each.js b/lib/handlebars/helpers/each.js index 9fc5a095d..d39a30098 100644 --- a/lib/handlebars/helpers/each.js +++ b/lib/handlebars/helpers/each.js @@ -25,6 +25,12 @@ export default function(instance) { } function execIteration(field, index, last) { + // Don't iterate over undefined values since we can't execute blocks against them + // in non-strict (js) mode. + if (context[field] == null) { + return; + } + if (data) { data.key = field; data.index = index; diff --git a/spec/regressions.js b/spec/regressions.js index 009fec90b..e8942a484 100644 --- a/spec/regressions.js +++ b/spec/regressions.js @@ -196,4 +196,11 @@ describe('Regressions', function() { shouldCompileToWithPartials(root, [{}, helpers, partials], true, ''); }); + + it('GH-1065: Sparse arrays', function() { + var array = []; + array[1] = 'foo'; + array[3] = 'bar'; + shouldCompileTo('{{#each array}}{{@index}}{{.}}{{/each}}', {array: array}, '1foo3bar'); + }); });