2

I’m using an external service to pull the events of an organisation (GetEvents). Example of what is returned:

{
   "Result":"success",
   "Key":"12345",
   "Data":[
     {"ID":"GFDCV34","lastChangedDate":"2015-12-03 11:14:27"},
     {"ID":"IDJHE23","lastChangedDate":"2015-12-03 15:17:47"},
     {"ID":"KDJBD34","lastChangedDate":"2015-12-03 05:25:11"}
   ]
}

Next, I can pull details of a certain event (GetEventDetails). The ID of the event is required as data parameter. I made a function getdetails(id); that returns the details. For example, for getdetails('KDJBD34'), it gives me:

{
  "Result":"success",
  "Key":"52523",
  "Data":[
    {
      "ID": "KDJBD34",
      "name": "Name of event 3",
      "date": "date of event 3",
      "location": "location of event 3",
      "lastChangedDate":"2015-12-03 05:25:11"
     }
   ]
}

I want to construct an array containing all the events and their details, like this:

{
  "Result": "success",
  "Key": "12345",
  "Data":[
    {
     "ID": "GFDCV34",
     "name": "Name of event 1",
     "date": "date of event 1",
     "location": "location of event 1",
     "lastChangedDate": "2015-12-03 11:14:27"
    },
    {
      "ID": "IDJHE23",
      "name": "Name of event 2",
      "date": "date of event 2",
      "location": "location of event 2",
      "lastChangedDate": "2015-12-03 15:17:47"
    },
    {
      "ID": "KDJBD34",
      "name": "Name of event 3",
      "date": "date of event 3",
      "location": "location of event 3",
      "lastChangedDate":"2015-12-03 05:25:11"
    }
  ]
}

Anyone who can point me in the right direction?

Gearnode
  • 693
  • 7
  • 21
binoculars
  • 2,226
  • 5
  • 33
  • 61

1 Answers1

1

You should operate through your first results and attach the new retrieved properties

var res = {
  "Result": "success",
  "Key": "12345",
  "Data": [{
    "ID": "GFDCV34",
    "lastChangedDate": "2015-12-03 11:14:27"
  }, {
    "ID": "IDJHE23",
    "lastChangedDate": "2015-12-03 15:17:47"
  }, {
    "ID": "KDJBD34",
    "lastChangedDate": "2015-12-03 05:25:11"
  }]
};

var tmp;
res.Data.map(function(val,i){
    tmp = getdetails(val.ID);
    Object.keys(tmp.Data[0]).map(function(v,j){
    val[v] = tmp.Data[0][v];
  });

});

Demo

vorillaz
  • 6,098
  • 2
  • 30
  • 46
  • Thanks for your reply. I can't get it to work... maybe because I actually get details of a meeting using `getdetails('KDJBD34').then(function(data){ var details = data; }, function(error){ console.log(error) });` instead of `getdetails('KDJBD34')`. – binoculars Dec 28 '15 at 20:24
  • So I tried updating you code to: `var tmp; res.Data.map(function(val,i){ getdetails(val.ID).then(function(data){ tmp = JSON.parse(data); Object.keys(tmp.Data).map(function(v,j){ val[v] = tmp.Data[v]; }); }, function(error){ console.log(error) }); });` , but doing it this way, the details aren't there... any ideas? – binoculars Dec 28 '15 at 20:24
  • @binoculars Can you give us a sample JSfiddle? – vorillaz Dec 29 '15 at 10:24
  • I've opened a new question to address this issue: http://stackoverflow.com/questions/34507973/asynchronous-javascript-issue/34508789#34508789 – binoculars Dec 29 '15 at 10:39