简体   繁体   English

如何使用JavaScript将单词包装在单个冒号和逗号分隔的单词中

[英]How to wrap word in single colon and separate words with comma using javascript

I have around 100 word in following format abc 我有以下格式的abc约100个字
def 高清
eft
fgg FGG
asd ASD
and so on....till n numbers 依此类推...直到n个数字

I want it to be like 'abc','def','eft' and so on... 我希望它像'abc','def','eft'等...

Basically i am trying to insert a row in database. 基本上我想在数据库中插入一行。

I am really confused. 我真的很困惑。 Can you guys help me out with logic. 你们能用逻辑帮助我吗? I have huge list cant do manually. 我有大量清单无法手动执行。

Here's one of many ways to do it 这是多种方法之一

var source = document.querySelector("pre").textContent;

var items = source.split("\n"); //Split items by new line
items = items.map(function(item) { //Trim whitespace off of each item
    return item.trim(); 
}).filter(function(item) { //Filter out empty items
    return item;
});

var list = "'" + items.join("','") + "'"; //Join items together by single quotes and comas, and add single quotes at the beggining and the end
alert(list);

http://jsfiddle.net/m1n7xc6r/ http://jsfiddle.net/m1n7xc6r/

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

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