I have an object that looks like this:
{
\"id\": \"123\",
\"members\": [
{ \"id\": 1, \"name\": \"Andrew\" },
{ \"id\": 2, \"name\": \"Jim\" }
something like
var arr = {
"id": "123",
"members": [
{ "id": 1, "name": "Andrew" },
{ "id": 2, "name": "Jim" }
]
};
console.log( arr.members.reduce( function(x,y){ return "\"" + x.name + "\", \"" + y.name + "\"" } ) );
members
is Array
of Objects
, first you need to create Array
with names - for this case you can use .map and then convert this Array
to String
- to do that you can use .join
var data = {
"id": "123",
"members": [
{ "id": 1, "name": "Andrew" },
{ "id": 2, "name": "Jim" }
]
};
var result = data.members.map(function (e) {
return e.name;
}).join(', ');
console.log(result);