Reputation: 3
I am new into Javascript and actually I'm facing following problem. I get JSON-object by calling an API. I get more than one object, that is fine. The objects are like this:
{"version": 1.0.1,
"id": 125,
"name": "Elmos App Test",
"creationDate": "2017-05-28",
},
{"version": 1.0.4,
"id": 25,
"name": "Elmos App Prod",
"creationDate": "2017-05-25",
},
{"version": 1.1,
"id": 14,
"name": "Elmos App Int",
"creationDate": "2017-04-23",
}
I hope it is not too difficult to identify the JSON here. MY problem is now to save the three names in one variable.
Response of call should be like --> "Your apps are Elmos App Test Elmos App Prod Elmos App Int"
I actually have following Javascriptcode:
function getJSON(callback){
request.get(url(), function(error, response, body){
var d = JSON.parse(body);
var result = d.name;//query for result
if (result > null){
callback(result);}
else
{
callback("ERROR");
}
});
As you can see i try to save the name into the var result. Hope someone can help me out there. Thank you.
Upvotes: 0
Views: 75
Reputation: 13943
You can use Array#map()
to only get the names and then join()
to concatenate them
const array = [{"version": "1.0.1", "id": 125, "name": "Elmos App Test", "creationDate": "2017-05-28", }, {"version": "1.0.4", "id": 25, "name": "Elmos App Prod", "creationDate": "2017-05-25", }, {"version": "1.1", "id": 14, "name": "Elmos App Int", "creationDate": "2017-04-23" }],
name = array.map(a=>a.name).join(' ');
console.log(name);
Upvotes: 3