首页 > 解决方案 > 删除数组内的多个双引号

问题描述

我有一个包含 5 个元素和 4 个双引号的数组,如何在保留其余元素的同时仅删除双引号。

let arr = ["kot"," "," ",","dart","," "];

标签: javascript

解决方案


you can make use of String.trim and Array.filter

arr = arr.filter(element => element.trim())

the trim method will remove starting and trailing whitespace, so the " " strings will be converted to "". Empty strings are considered falsy, so they will be removed by the filter function.

let arr = ["ruby","javascript","golang","dart","kotlin"," "," "," "," "];

console.log(arr.filter(e => e.trim()));


推荐阅读