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?

1
  • 1
    var event = []; events.push(getdetails('KDJBD34')); Commented Dec 28, 2015 at 17:34

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

4 Comments

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').
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 Can you give us a sample JSfiddle?
I've opened a new question to address this issue: stackoverflow.com/questions/34507973/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.