SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Addict skunkbad's Avatar
    Join Date
    Apr 2008
    Location
    Temecula, CA
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Pushing to array that is inside an object

    Input is 3 rows with ids
    1) a-123123
    2) a-543543
    3) b-543543
    Code:
    $('.bg_upload_row').each(function(){
    
    	var row_id = $(this).attr('id').split('-');
    	// row_id[0] is the type
    	// row_id[1] is the user ID
    
    	// Push user ID into user set if it isn't already there
    	if( $.inArray( row_id[1], user_set ) === -1 ){
    		user_set[row_id[1]] = [];
    	}
    
    	// Push upload type into uploaded set for user
    	user_set[row_id[1]].push(row_id[0]);
    });
    
    console.log( user_set );
    What I'm hoping for would be an object with arrays of types, like this:
    Code:
    {
    	"123123": ["a"],
    	"543543": ["a","b"]
    }
    But the second type is overwriting the first, so the arrays are always an array with a single value, like this:
    Code:
    {
    	"123123": ["a"],
    	"543543": ["b"]
    }
    And I'm not even sure if I'm going about things the right way for what I'm doing. Basically, the types are upload types, and the user must upload a specific set of types, or they can't get past a certain point. I think if I can get this object of arrays created I'll be able to further evaluate if all of the users have all of their uploads, but I'm not successful yet. I'm used to PHP, and making a multi-dimensional array in PHP is a piece of cake, but javascript is not so friendly for me.

  2. #2
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,189
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The problem is your IF statement, the $.inArray method checks for values within the array but not the keys themselves so there for the new array is been created everytime. Instead you should use the below:

    Code JavaScript:
    if (!row_id[1] in user_set) {
        user_set[row_id[1]] = [];
    }


Tags for this Thread

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
  •