简体   繁体   English

在字符串数组 javascript 中添加单引号

[英]Add single quotes in a string array javascript

I been searching for the solution but can find it...我一直在寻找解决方案,但可以找到它......

I have the following array with the following output:我有以下数组,其中包含以下 output:

['Fruits, Cars, Clothes, Shoes'] ['水果、汽车、衣服、鞋子']

But I need the that array to output the following:但我需要该数组到 output 以下:

['Fruits', 'Cars', 'Clothes', 'Shoes'] ['水果','汽车','衣服','鞋子']

I already tried the following and it did not work:我已经尝试了以下方法,但没有奏效:

var output= arr.map(item => "'" + item + "'").join(); var output= arr.map(item => "'" + item + "'").join();

Can you help please你能帮忙吗

Just get the first (and unique) item of your array, then split it and trim every element:只需获取数组的第一个(也是唯一的)项目,然后split它并trim每个元素:

 const arr = ['Fruits, Cars, Clothes, Shoes']; const newArr = arr[0].split(',').map(x => x.trim()); console.log(newArr);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM