I have the following json:
{
'a':'vala',
'b':'valb'
}
I want to convert this to a string:
"a=vala,b=valb"
What is the best way to get there?
I have the following json:
{
'a':'vala',
'b':'valb'
}
I want to convert this to a string:
"a=vala,b=valb"
What is the best way to get there?
Use Object.keys to get all the keys, map over the results to format the string, and finally join using a comma separator.
var item = {
'a':'vala',
'b':'valb'
};
var result = Object.keys(item).map(function(key) {
return key + '=' + item[key];
}).join(',');
Another way you could do it--all it takes is a simple for in loop on the object with some formatting for your results:
var obj = {
'a':'vala',
'b':'valb'
};
function translate(obj) {
var result = [];
for (var key in obj) {
result.push(key + '=' + obj[key]);
}
return result.join(',')
}
console.log(translate(obj));