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.

4
  • How your output should look like ? Commented Dec 16, 2015 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 stackoverflow.com/questions/5181493/… Commented Dec 16, 2015 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? Commented Dec 16, 2015 at 10:54
  • The output of the whole task looks like this I marked the resources with red Commented Dec 16, 2015 at 11:07

4 Answers 4

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
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just added the calls to toString - because in your sample one is a string and the other is a number.
Sir you won today :) This works! (used the first one, only instead of vars i used lets ;) ). Cheers!
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.
    }
}

Comments

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

1 Comment

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.
-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

1 Comment

Gonna try this approach right away, I'll let you know the outcome ;)

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.