1

More or less the situation is like this: I get an array (using JS) and one object (let's call it TASK) looks like this (it's not the full array, just ONE instance):

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

The other object that I load contains all "resources", referred in the first array with "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"
  }
]

I populate some form fields with task data, but there is simply no way I can figure out how to "connect" the task with its resources.

halfer
  • 19,824
  • 17
  • 99
  • 186
Edi Smajic
  • 85
  • 4
  • How your output should look like ? – Rayon Dec 16 '15 at 10:50
  • I am not 100% sure I understand but looks like you need to get `task.assigs.resourceId` inside an each loop and then use the value to find the `resources.id` like in this question http://stackoverflow.com/questions/5181493/how-to-find-a-value-in-a-multidimensional-object-array-in-javascript – lukehillonline Dec 16 '15 at 10:53
  • Are you looking for something like a function which can update your resources array on the basis of your action in form to assign them to a task? – Aditya Sethi Dec 16 '15 at 10:54
  • The output of the whole task looks like [this](http://i.imgur.com/4e5ubLY.png) I marked the resources with red – Edi Smajic Dec 16 '15 at 11:07

4 Answers4

1

Like this if we are talking newer versions of 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
            }
        }
    }
}

In all versions of JS you can do it like this

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
            }
        }
    }
}
Christian Nielsen
  • 599
  • 1
  • 4
  • 15
0

This might not be the best answer there is, but I'd probably use the the filter().

You loop through all your tasks (in a foreach loop for instance), and then you loop through each assig(?), and then do:

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.
    }
}
blas3nik
  • 1,381
  • 11
  • 21
0

What if instead of "resourceId" you just set your Resource object in there? You will have your connection:

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

Or, if you want to keep your structure, just add a reference to object that exists in the Resources array:

"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
    }
],
Cesar
  • 420
  • 6
  • 12
  • 1
    This is a client side code, although you can make your backend write it on your page. But the point is, you can connect/link your array items to other array items. I hope this is what you are looking for. – Cesar Dec 16 '15 at 11:56
-1

I'm not sure whether or not this is what you're looking for, but you should be able to loop through the Task array, and inside that loop, loop through the assigs array, and inside this loop, loop through your Resources array. Doing this, you can find the connection.

Example:

//Loop through Task array
for(i = 0; i < Task.length; i++){
    //Loop through your assigs within the Task array, at the given index 'i'
    for(j =0; j < Task[i].assigs; j++){
        //Now loop through the Resources array, to find the ID that matches the assigs 'resourceId'
        for(k =0; k < Resources.length; k++){
            if(Task[i].assigs[j].resourceId === Resources[k].ID){
                //You have the connection
            }
        }
    }
}

NOTE: this could probably be optimized fairly much, but at least here's the principal

EDIT: or as Christian Nielsen does, using a foreach loop

halfer
  • 19,824
  • 17
  • 99
  • 186
Detilium
  • 2,868
  • 9
  • 30
  • 65