Remove Whitespace-only Array Elements

后端 未结 8 880
悲&欢浪女
悲&欢浪女 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 18:05

    const words = ['spray', 'limit', 'elite', 'exuberant', ' ', ''];
    
    const result = words.filter(word => word.trim().length > 0);
    
    console.log(result);

    0 讨论(0)
  • 2020-12-08 18:06

    Here's another approach.

    var data = JSON.parse(arrayval.split(/\s/).join(''));
    
    0 讨论(0)
提交回复
热议问题