SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,727
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    forEach object array problem angularjs

    Hi all

    I need help understanding the below snippet and getting it to function within my existing code.

    My current project can be found here http://jsfiddle.net/umc22/4/

    In total, there are 11 available genres within the arrays which should be listed in the dropdown, I only see 4 all saying [object] ?
    I know the $scope.init = function() is causing the problem, if you check the link above everything should become clear.

    Could anybody explain exactly what this code is doing?
    function(genre, index) - what does the index mean?

    Code JavaScript:
    $scope.init = function() {
            angular.forEach(Venues.list, function(genre, index){
                //Only add to the availableGenres array if it doesn't already exist
                var exists = false;
                angular.forEach($scope.availableGenres, function(avGenre, index){
                    if (avGenre == genre) {
                        exists = true;
                    }
                });
                if (exists === false) {
                    $scope.availableGenres.push(genre);
                }
            });
        };
     
    $scope.setGenreFilter = function(genre) {
            $scope.genreFilter = genre;
        };

    Help much appreciated.
    Still a javascript novice and learning as I go along.

    thanks, cb
    The more you learn.... the more you learn there is more to learn.

  2. #2
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,727
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anybody

    Code JavaScript:
    angular.forEach(Venues.list, function(genre, index){

    .list is returning four objects as below, it should be returning 11 genres?

    http://jsfiddle.net/umc22/5/

    [object Object]
    [object Object]
    [object Object]
    [object Object]


    Thanks, cb
    The more you learn.... the more you learn there is more to learn.

  3. #3
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,313
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    angular.forEach(Venues.list, function(genre, index){
    You're iterating over the list of venues, and assigning each venue to a variable named "genre". You're getting [object Object] because what you think is a genre is actually a venue object. You'll need one more forEach.

    Code:
            angular.forEach(Venues.list, function(venue, index){
                angular.forEach(venue.genres, function (genre, index) {
                    //Only add to the availableGenres array if it doesn't already exist
                    var exists = false;
                    angular.forEach($scope.availableGenres, function(avGenre, index){
                        if (avGenre == genre) {
                            exists = true;
                        }
                    });
                    if (exists === false) {
                        $scope.availableGenres.push(genre);
                    }
                });
            });
    "First make it work. Then make it better."

  4. #4
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,727
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Works brilliant!
    Can't thank you enough Jeff been working on this on and off 10+ days trying to understand what's happening.

    You make it look so easy
    Still learning javascript good learning exercise thanks.

    update: http://jsfiddle.net/umc22/7/

    I'll repost if I have any further issues.

    Cheers and Merry Christmas!

    PS.
    what exactly does ... (genre, index) the index mean, what does it do?
    And whats the difference between $index and index?

    If you have a minute thanks.
    The more you learn.... the more you learn there is more to learn.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •