If you want to map objects to something (in this case a property). I think Array.prototype.map is what you're looking for if you want to code functionally.
(fiddle)
If you want to support older browsers, that are not ES5 compliant you can shim it (there is a polyfill on the MDN page above). Another alternative would be to use underscorejs's pluck method:
var users = [
{name: "Joe", age: 22},
{name: "Kevin", age: 24},
{name: "Peter", age: 21}
];
var result = _.pluck(users,'name').join(",")
Answer from Benjamin Gruenbaum on Stack OverflowIf you want to map objects to something (in this case a property). I think Array.prototype.map is what you're looking for if you want to code functionally.
(fiddle)
If you want to support older browsers, that are not ES5 compliant you can shim it (there is a polyfill on the MDN page above). Another alternative would be to use underscorejs's pluck method:
var users = [
{name: "Joe", age: 22},
{name: "Kevin", age: 24},
{name: "Peter", age: 21}
];
var result = _.pluck(users,'name').join(",")
Well you can always override the toString method of your objects:
var arr = [
{name: "Joe", age: 22, toString: function(){return this.name;}},
{name: "Kevin", age: 24, toString: function(){return this.name;}},
{name: "Peter", age: 21, toString: function(){return this.name;}}
];
var result = arr.join(", ");
console.log(result);
Run code snippetEdit code snippet Hide Results Copy to answer Expand
javascript - Combine object key values into single string with separator - Stack Overflow
javascript - How to join values of one object? - Stack Overflow
javascript - How to join object values into a string? join() for objects? - Stack Overflow
javascript - Join JSON Object member string values together - Stack Overflow
You can use Object.values and map: Objet.values returns an array of values in the object and join converts these values to string by joining them by ',' here.
const users = [{
name: "Jon",
age: 34,
country: "ES"
}];
var result = users.map(u => Object.values(u).join(','));
console.log(result);
You can use Object.values to get an array of the properties of your object, then use the join method of that array to create a string from the values:
var result = users.map(function (a) {
return Object.values(a).join(', ');
});
console.log(result[0]); // Jon, 34, ES
This looks like a job for $.map!
var data = {
"category": [{
"id": 28,
"name": "Dogs"
},
{
"id": 14,
"name": "Cats"
},
{
"id": 878,
"name": "Sheep"
}]
}
var cats = $.map(data.category, function(v){
return v.name;
}).join(', ');
var text = "";
for(var i=0; category.length; i++)
{
text += category[i].name;
if(i!=category.length-1)
text += ", ";
}
I'm getting an array like this [ {name: 'a', value: 'b'}, {name: 'c', value: 'd'}, {name: 'e', value: 'f'} ]. I need to convert this array to a string, like 'a=b/c=d/e=f' to send to an API. How can I do this? I have tried all sorts of Array map tricks and Object.values or keys stuff to no avail.
One possible approach:
var tagsArray = {};
tagsArray["1"] = "one";
tagsArray["2"] = "two";
tagsArray["Z"] = "zed";
var result = Object.values(tagsArray).join(",");
console.log(result); // "one,two,zed"
More on Array.prototype.join and Object.values.
With Object.values you can get values as an array, and then just need to pass it to string:
Object.values(tagsArray).toString();