Javascript - Loop through sparse array and replace sparse values -
i'm trying loop through sparse array , fill in sparse elements value.
['foo', 'bar', , , ,].map(el => el || 'default') // returns ["foo", "bar", undefined × 3]
how return ["foo", "bar", "default", "default", "default", "default"]
since .map (and .foreach) skip sparse values there's no option except use loop, should explicitly check absence of missing keys
for (var = 0, n = a.length; < n; ++i) { if (!(i in a)) { // explicit check missing sparse value a[i] = "default"; } }
Comments
Post a Comment