I\'m trying to loop through a sparse array and fill in sparse elements with a value.
[\'foo\', \'bar\', , , ,].map(el => el || \'default\') // returns
[\'foo\', \'bar\', , , ,].map(el => el || \'default\')
This should do it:
function Fill(n, _default) { return Array.apply(null,n).map(function(val) { return val || _default; }); } var newa = Fill(myarray, "default"); console.log(JSON.stringify(newa));
Shown working here: https://jsfiddle.net/6vqsztxg/