SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2010
    Posts
    33
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    javaScript adding array to another array

    Hi javascipt gurus,

    I have a issue that is getting me frustrated I a large arrays of profiles. So I take the necessary arrays out of profile and add them to an small array called items. Then I want to take elements out of the new items array and place the string values into a select array the issue is the default if in the last for loop it will not work it only ever returns the entire string/ array. Any help would be great.


    Code:
    var select =[];
    
    var profile = [];
    profile["0"] = ['240','1','4','1','1','1','2.40','CR240'];
    profile["1"] = ['360','3','5','1','1','1','3.60','CR360'];
    profile["2"] = ['480','4','6','1','1','1','4.80','CR480'];
    
    var items[];
    var x= 0;
    	
    	for(var i=0;i<profile.length;i++){
    		
    
    		if(profile[i][2] == itemVar){
    			
    			 items[x]= hc_styleprofile[i];
    			 
    		x++;
    		}
    
    	}
    
    alert(item[0]);
    
    var defaultItem = 2;
    
    	for(var i=0;i<item.length;i++){
    		alert(item[i]);
    		if(defaultItem == item[i]){
    			select["default_size"]=items[i];
    			select['default_size']);
    
    
    		}
    	
    	}

  2. #2
    SitePoint Member
    Join Date
    May 2011
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look up info on forEach Object

    https://developer.mozilla.org/en/Jav.../Array/forEach

    only got it to work for firefox...

    let me know if it helps...

  3. #3
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    I am sure this a typo:
    Code:
    select['default_size']);
    You also have item & items variables - same?

    Here is a cleaned up version of the code you provided:
    Code JavaScript:
    var select = [],
    	profile = [],
    	items = [],
    	defaultItem = 2,
    	x = 0,
    	i;
     
    profile["0"] = ['240','1','4','1','1','1','2.40','CR240'];
    profile["1"] = ['360','3','5','1','1','1','3.60','CR360'];
    profile["2"] = ['480','4','6','1','1','1','4.80','CR480'];
     
    // Not defined in the code provided
    var itemVar, hc_styleprofile;
     
    for(i = 0; i < profile.length; i++) {
    	if(profile[i][2] === itemVar) {
    		items[x] = hc_styleprofile[i];
    		x++;
    	}
    }
     
    // Item is not defined....
    var item = [];
     
    alert(item[0]);
    for(i = 0; i < item.length; i++) {
    	alert(item[i]);
    	if(defaultItem === item[i]) {
    		select["default_size"] = items[i];
    	//  Typo?
    	//	select['default_size']);
    	}
    }

    I believe the last section should have all items and not item/s:
    Code JavaScript:
    for(i = 0; i < items.length; i++) {
    	if(defaultItem === items[i]) {
    		select["default_size"] = items[i];
    	}
    }


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
  •