Convert object's properties and values to array of key value pairs
You're probably looking for something along the lines of
var obj = {value1: 'prop1', value2: 'prop2', value3: 'prop3'};
var arr = [];
for (var key in obj) {
if (obj.hasOwnProperty(key)) {
arr.push(key + '=' + obj[key]);
}
};
var result = arr.join(',');
alert(result);
Notice that it will work fine if your values are strings; if they're complex objects then you'll need to add more code.
Or you can just use jQuery.param, which does what you want, even for complex types (although it uses the &
character as the separator, instead of the comma.
In ES6 you can use Object.entries({object1:1,object2:2});
. The result is: [["object1",1],["object2",2]]
var array = [];
for (k in o)
{
if (o.hasOwnProperty(k))
{
array.push(k+"="+o[k]);
}
}
You can then join
the array for your final string.