0

I am working on a project which allows users to filter jobs based on criteria such as city, job level and job category. As the user clicks on a filter the search results will be filtered, but so too will the filters themselves.

To do this I plan to get my list of filter cities, levels and categories from the filtered job objects. My question is how, in Angular, can I create 3 object arrays using data from a 4th object array?

My Jobs array ($scope.programs) has the following format:

[
    {
        title: "HR Analyst",
        city: "Jacksonville",
        state: "Florida",
        country: "United States",
        region: "North America",
        level: "Full Time Analyst",
        category: " Human Resources"
    }
]

and I then plan to create 3 object arrays which store the name of the filter and its availability. So $scope.cities might look like this:

[
    {
        name: "London",
        available: "true",
    },
    {
        name: "Sydney",
        available: "false",
    },
]
12
  • 1
    Are you asking someone to write you a function that will take one object array as an input and return 3 new arrays as output? You state that your question is whether or not it's possible to "create 3 object arrays using data from a 4th object array." Why would you think this wouldn't be possible? What have you tried so far to achieve your desired effect? Why did it not work? Commented Jun 14, 2016 at 20:01
  • Is frontend best place to do those calculations? Commented Jun 14, 2016 at 20:04
  • Hi Dan, I do think its possible. I'm just not sure how to go about it, hence me asking for help! I know that I will need to use a forEach loop to loop through the items in scope.programs but Im not sure how to create the other object arrays and assign program.city from the programs array (for instance) to city.name in the cities array. Commented Jun 14, 2016 at 20:09
  • Okay, so then what about the set of questions that appears at the end of my comment. What have you tried so far to achieve your desired effect? Why did it not work? It sounds like you might want to read up on working with arrays and objects in Javascript. Commented Jun 14, 2016 at 20:11
  • 1
    Ok maybe I'll read up on that then. Forgive me, this is my first Angular project so I'm still not familiar with the "Angular way" of doing things yet. Commented Jun 14, 2016 at 20:30

1 Answer 1

1

I managed to populate my arrays with:

$scope.getFilters = function(){
    angular.forEach($scope.programs, function(program) {
        $scope.filterCities.push({
            name: program.city,
            available: true
        });
        $scope.filterCategories.push({
            name: program.category,
            available: true
        });
        $scope.filterLevels.push({
            name: program.level,
            available: true
        });
    });

    console.log($scope.filterCities);
    console.log($scope.filterCategories);
    console.log($scope.filterLevels);

};

Although after speaking with Dan Forbes I believe I should restrict my Cities, Categories and Levels using custom Angular filters rather than creating arrays. I hope this helps someone.

Sign up to request clarification or add additional context in comments.

Comments

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.