构建多维数组了其他阵列 [英] Constructing multidimensional array out of other arrays

查看:144
本文介绍了构建多维数组了其他阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用外部服务来拉一个组织(GetEvents)的事件。被返回的示例:

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"}
   ]
}

接下来,我可以拉一个特定事件(GetEventDetails)的详细信息。事件的ID是必需的数据参数。我做了一个函数 getdetails(ID); 返回的细节。
例如, getdetails('KDJBD34'),它给我:

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?

推荐答案

您应该通过你的第一个结果操作和附加新的检索性能

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];
  });

});

演示

这篇关于构建多维数组了其他阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆