Building a multidimensional array from other arrays

I use an external service to pull organization events (GetEvents). An 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 find the details of a specific event (GetEventDetails). An event identifier is required as a data parameter. I made a function getdetails(id);that returns the details. For example, for getdetails('KDJBD34')he 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 build an array containing all the events and their details, for example:

{
  "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?

+2
source share
1 answer

You must work with your first results and attach new properties found.

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

+1
source

Source: https://habr.com/ru/post/1622061/


All Articles