简体   繁体   中英

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

I have around 100 word in following format abc
def
eft
fgg
asd
and so on....till n numbers

I want it to be like 'abc','def','eft' and so on...

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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