Javascript - Loop through sparse array and replace sparse values

后端 未结 3 995
既然无缘
既然无缘 2021-01-21 04:54

I\'m trying to loop through a sparse array and fill in sparse elements with a value.

[\'foo\', \'bar\', , , ,].map(el => el || \'default\') // returns

3条回答
  •  清歌不尽
    2021-01-21 05:32

    Just use a for loop:

    for (i = 0; i < arr.length; i++) {
      if (arr[i] === undefined) 
        arr[i] = 'default'
    }  
    

提交回复
热议问题