Remove Whitespace-only Array Elements

后端 未结 8 915
悲&欢浪女
悲&欢浪女 2020-12-08 17:30

Since using array.splice modifies the array in-place, how can I remove all whitespace-only elements from an array without throwing an error? With PHP we have preg_grep but I

8条回答
  •  星月不相逢
    2020-12-08 17:59

    And for a new generation (namely ES2015):

    ['1', ' ', 'c'].filter(item => item.trim() !== '')
    

    More on trim()

提交回复
热议问题