简体   繁体   中英

Add two quotes in JavaScript

I have a code like this :

  var event = { "temperature" : 12.45, "humidite" : 45.78, "built" : "charone", "fire" : "chartwo", "topic" : "sk1_001/data" }; var keys = Object.keys(event); valeurs_d = ""; for (var i = 0; i < keys.length -1 ; i++) valeurs_d += event[keys[i]] + ", "; var str5 = ","; var str6 = str5.concat(valeurs_d); var valeurs = str6.substring (0, str6.length - 2); console.log(valeurs); 

I want to have everything between two quotes in variable valeurs like this:

, '12 .45 ', '45 .78', 'charone', 'chartwo'

Or, just have the character strings between two quotes:

, 12.45, 45.78, 'charone', 'chartwo'

I can have in my JSON other strings.

How can I do this in JavaScript?

Quite simply,

  var event = { "temperature" : 12.45, "humidite" : 45.78, "built" : "charone", "fire" : "chartwo", "topic" : "sk1_001/data" }; var keys = Object.keys(event); valeurs_d = ""; for (var i = 0; i < keys.length -1 ; i++) valeurs_d += "\\'" + event[keys[i]]+ "\\'" + ", "; var str5 = ","; var str6 = str5.concat(valeurs_d); var valeurs = str6.substring(0, str6.length - 2); console.log(valeurs); 

Just escape those single quotes and concatenate them to your values.

If you want a double quote in your string, then use "\\"" instead of "\\'" .

You can add the quotes directly into the string that you are building, like in this example:

 var event = { "temperature": 12.45, "humidite": 45.78, "built": "charone", "fire": "chartwo", "topic": "sk1_001/data" }; var keys = Object.keys(event); var valeurs_d = ""; for (var i = 0; i < keys.length - 1; i++) { valeurs_d += "'" + event[keys[i]] + "', "; } console.info(valeurs_d); 

If you're using a recent version of most major browsers, you can just use Object.values() :

 var event = { "temperature" : 12.45, "humidite" : 45.78, "built" : "charone", "fire" : "chartwo", "topic" : "sk1_001/data" }; console.log(Object.values(event)); 

If you really want the numbers as strings too, change the last line to console.log(Object.values(event).map(value => String(value))) .

And if you need the initial comma, just add it: ', ' + Object.values(event).map(value => String(value)) .

You could check the type and add some quotes around.

 var event = { temperature : 12.45, humidite : 45.78, built : "charone", fire : "chartwo", topic : "sk1_001/data" }, result = ',' + Object .keys(event) .slice(0, -1) .map(k => typeof event[k] === 'string' ? '\\'' + event[k] + '\\'' : event[k]) .join(','); console.log(result); 

Try replacing this line:

valeurs_d += event[keys[i]] + ", ";

for this one

valeurs_d += "'" + event[keys[i]] + "', ";

Consider this if it is a string:

var str = "chartwo"
var str_with_qoutes =  "'" + str + "'";

If interpret Question correctly, you can use Object.values() , Array.prototype.join() with parameter "','" , template literal

  var event = { "temperature" : 12.45, "humidite" : 45.78, "built" : "charone", "fire" : "chartwo", "topic" : "sk1_001/data" }; var valeurs = `",'${Object.values(event).join("','")}'"`; console.log(valeurs); 

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