首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >按ID从一个数组获取另一个数组的数据

按ID从一个数组获取另一个数组的数据
EN

Stack Overflow用户
提问于 2015-12-16 18:48:04
回答 3查看 544关注 0票数 1

或多或少的情况是这样的:我得到一个数组(使用JS),一个对象(让我们称之为任务)看起来像这样(它不是完整的数组,只有一个实例):

{
      "id": "28",
      "name": "sdfsdf",
      "progress": 80,
      "description": "",
      "code": "1",
      "level": 1,
      "status": "STATUS_SUSPENDED",
      "depends": "",
      "canWrite": true,
      "start": 1444341600000,
      "duration": 7,
      "end": 1445291999999,
      "startIsMilestone": 0,
      "endIsMilestone": 0,
      "collapsed": false,
      "assigs": [
        {
          "resourceId": 3,
          "otherStuff": xyz
        },
        {
          "resourceId": 2,
          "otherStuff": xyz
        }
      ],
      "hasChild": true
    }

我加载的另一个对象包含所有“资源”,在第一个数组中用“assigs”引用:let's call these RESOURCES

[
  {
    "ID": "1",
    "name": "service | 1st resource we need",
    "unit": "pcs",
    "quantity": "10"
  },
  {
    "ID": "2",
    "name": "money | Office space",
    "unit": "hour",
    "quantity": "50"
  },
  {
    "ID": "3",
    "name": "product | Money for nothing...",
    "unit": "$",
    "quantity": "300"
  },
  {
    "ID": "4",
    "name": "people | Chovjek",
    "unit": "people",
    "quantity": "1"
  }
]

我用任务数据填充了一些表单域,但根本无法弄清楚如何将任务与其资源“连接”起来。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-16 18:57:10

如果我们讨论的是较新版本的javascript,如下所示:

for(var task of tasks)
{
    for(var ass of task.assigns)
    {
        for(var res of resources)
        {
            if(res.ID === ass.resourceId.toString()) {
                //here res and ass match and you can do what you want
            }
        }
    }
}

在所有版本的JS中,您都可以这样做

for(var i = 0; i < tasks.length; i++)
{
    for(var x = 0; x< tasks[i].assigns.length; x++)
    {
        for(var y = 0; y < resources.length; y++)
        {
            if(resources[y].ID === tasks[i].assigns[x].resourceId.toString()) {
                //here res and ass match and you can do what you want
            }
        }
    }
}
票数 1
EN

Stack Overflow用户

发布于 2015-12-16 18:58:07

这可能不是最好的答案,但我可能会使用filter()

您遍历所有任务(例如,在foreach循环中),然后遍历每个assig(?),然后执行以下操作:

for (var task of tasks) {
    for (var currentAssig of assigs) {
        var resourceId = currentAssig.resourceId;
        var relevantResource = RESOURCES.filter(function (res) {
            return res.ID === resourceId;
        });
        // do whatever you want with your resource.
    }
}
票数 0
EN

Stack Overflow用户

发布于 2015-12-16 19:07:28

如果你只是在其中设置你的资源对象而不是"resourceId“,会怎么样呢?您将拥有您的连接:

{
  "id": "28",
  "name": "sdfsdf",
  /* .... */
  "assigs": [
    {
      "resource":
       {
            "ID": "1",
            "name": "service | 1st resource we need",
            "unit": "pcs",
            "quantity": "10"
       },
      "otherStuff": xyz
    },
    {
      "resource":
       {
            "ID": "2",
            "name": "service | 2nd resource we need",
            "unit": "pcs",
            "quantity": "20"
       },
      "otherStuff": xyz
    }
  ],
  "hasChild": true
}

或者,如果您想保留结构,只需添加对Resources数组中存在的object的引用:

"assigs":
[
    {
      "resourceId": 3,
      "resource": yourResourceArray[0], //<-- [0] for sake of simplicity
      "otherStuff": xyz
    },
    {
      "resourceId": 2,
      "resource": yourResourceArray[1], //<-- [1] for sake of simplicity
      "otherStuff": xyz
    }
],
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34309963

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档