0

I have 2 arrays that i am returning...in this case Employments and Users. They both have a common field 'id' and i want to use that field to map.

I can use a for loop to map it currently but since i am looping over a nested array...i only get to catch the mapping for the first part of the array.

my json objects:

     $scope.Users = [{
          id: 1,
          name: "Ryan"
      }, {
          id: 2,
          name: "Julie"
      }, {
          id: 3,
          name: "Stu"
      }, 
      {
          id: 4,
          name: "Holly"
      }];

      $scope.Employments = [{
          categoriesBag: [
            {
              category: [
                {
                  user_id: 1,
                  title: "manager"
                },
                {
                  user_id: 2,
                  title: "student"
                }
              ]
            },
            {
              category: [
                {
                  user_id: 3,
                  title: "worker"
                },
                {
                  user_id: 4,
                  title: "facilty"
                }
              ]
            }
          ]
      }];

the for loop that i am using to map the data:

$scope.getCategory = function(id) {
        var employmentCategories =  $scope.Employments.categoriesBag[0].category;
        for (var i = 0; i < employmentCategories[0].category.length; i++) {
          if (employmentCategories[0].category[i].user_id === id) {
            return employmentCategories[0].category[i].title;
          }
        }
      };    

since i am specifying that i only want the first array employmentCategories[0], the other two users are not included in the for loop. Is there a way for me to do a loop inside of a loop to loop over only the nested categories?

3
  • 1
    What do you want the final merged array to look like? Commented Jun 26, 2017 at 16:03
  • Just do an outer loop for (var bag=0; bag<categoriesBag.length; bag++) {}... And instead of [0] uses [bag] Commented Jun 26, 2017 at 16:04
  • doesnt really matter...i just want to return the title of that employee based on the user id Commented Jun 26, 2017 at 16:04

1 Answer 1

1

You can use a nested loop

$scope.getCategory = function(id) {
    for (bag in $scope.Employments.categoriesBag) {
        for (category in bag.category) {
            if (category.user_id == id){
               return category.title
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

any idea how to turn this into a forEach?
forEach may not be best suited for searching objects in a list. This is because the callback will be called on all elements of the list even if the desired element is at the beginning of the list.

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.