js 数组去除空字符串
时间: 2023-10-20 10:02:46 浏览: 131
要在JavaScript数组中去除空字符串,可以使用filter方法。首先,使用split方法将字符串分割成数组。然后,使用filter方法对数组进行拷贝,删除空字符串元素,保留其他元素。具体实现代码如下所示:
```
const array = ["", "Hello", "", "World", "", ""];
const newArray = array.filter((element) => {
return element !== "";
});
console.log(newArray); // 输出: ["Hello", "World"]
```
在上面的例子中,我们创建了一个包含空字符串的数组。然后,使用filter方法对数组进行拷贝,并通过判断元素是否为空字符串来删除空字符串元素。最后,打印新数组,即去除了空字符串的数组。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [js去掉数组中的空字符串](https://blog.csdn.net/weixin_73757147/article/details/128871318)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [js去除字符串空格(空白符) && 将数组转化为字符串](https://blog.csdn.net/Maxueyingying/article/details/125765669)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文