Mona Coder
Mona Coder

Reputation: 6316

How to wrap groups of strings in single quotes

I need to export elements of an array to strings but keep the single quotes around the elements. As you can see, exporting them to the toString() method outputs Banana, Orange, Apple, Mango but I need to have them like 'Banana', 'Orange', 'Apple', 'Mango'.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var map = fruits.toString();

console.log(map);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

How can I do this?

Upvotes: 1

Views: 1382

Answers (5)

Radio
Radio

Reputation: 2853

I'd pause on the Array.map solution. A map to do a join operation works of course, but is a bit heavy handed. Granted we're in system nano time on small sets, but it is perhaps significant in some settings where processing time is critical. Converting large datasets to comma delimited strings, such as with CSV export is a common case. Good old fashioned join is tough to beat.

//edited with new times based on array of strings:

ES6 Array.map: 18.2150 ms

ES5 Array.map: 21.7800 ms

join: 3.5750 ms

var map, start, end, fruits;


fruits = [];
for(var i = 0; i < 50000; i++){
 fruits.push(i.toString());
}

start = performance.now();
var exportedFruits = fruits.map(fruit => "'" + fruit + "'");
exportedFruits = exportedFruits.toString();
end = performance.now();
console.log((end - start).toFixed(4)+" ms");

start = performance.now();
var exportedFruits = fruits.map(function(fruit){
  return "'" + fruit + "'";
});
exportedFruits = exportedFruits.toString();
end = performance.now();
console.log((end - start).toFixed(4)+" ms");


start = performance.now();
map = "'" + fruits.join("','") +"'";
end = performance.now();
console.log((end - start).toFixed(4)+" ms");
   

Edit: I forgot to measure the toString operation on the first two.

Upvotes: 0

artemnih
artemnih

Reputation: 4209

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var result = "'" + fruits.join("','") + "'";

Upvotes: 1

topheman
topheman

Reputation: 7902

ES6

var fruits = ["Banana", "Orange", "Apple", "Mango"];
// creates a new array containing: ["'Banana'", "'Orange'", "'Apple'", "'Mango'"]
var exportedFruits = fruits.map(fruit => "'" + fruit + "'");
console.log(exportedFruits.toString())

ES5

var fruits = ["Banana", "Orange", "Apple", "Mango"];
// creates a new array containing: ["'Banana'", "'Orange'", "'Apple'", "'Mango'"]
var exportedFruits = fruits.map(function(fruit){
  return "'" + fruit + "'";
});
console.log(exportedFruits.toString())

Upvotes: 3

Leeish
Leeish

Reputation: 5213

Here is one way.

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var map = fruits.map(myFun).toString();

console.log(map);
function myFun(val,i,array){
    return "'"+val+"'";
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Upvotes: 0

Jeremy J Starcher
Jeremy J Starcher

Reputation: 23863

Since you are using jQuery, you can use jQuery's map.

JavaScript ES6/ES2015 also has a Array.protytype.map which works very similar.

var arr = ['a', 'b', 'c'];

var res = $.map(arr, (function(val) {
  return "'" + val + "'";
  }));

console.log(res.join(","));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

Upvotes: 0

Related Questions