Javascript Arrays - Consolidating Multiple Arrays Help

Hi Guys,

I wonder if anybody could provide me with any assistance to the following problem I am having with my JS project?

I basically have information about a product being pulled from out from 3 different JSP lists:

  1. A Product’s Details
  2. A Products’s Ratings
  3. A Product’s Retailers

What I am trying to a do is loop through the three lists pulling out the relevant info for the product each time and then create a Javascript object which holds all the product information together.

Once created I then need to loop through this Javascript object and display each product’s information on screen - there could be 10, 20, etc, products at a time.

The area I am having trouble with is that when I create my ‘mainResults’ object (see my code snippet below), although it is being populated correctly, I get a blank array first and then the product arrays after that - see screenshot from firebug below:

http://www.vinternet-opensource.co.uk/images/screen.jpg

This is causing me problems when I try to loop through the ‘mainResults’ using its length property as it is always reurned as ‘0’.

I am sure there is better way to achieving what I need to do but after a lot of searching and playing around with OOP I still can’t find a good solution.

There are a couple of important things I need to add:

  • I will never know exactly how many products there will be.
  • The products have up to 3 ratings each.
  • I will never know exactly how many retailers a products has.
  • I need to access the product, its ratings and retailers by an index

I have posted the relevant parts of my code below - I really hope somebody can point me in the right direction :slight_smile:

Many thanks in advance,

Jim


        function populate(){
            <% if(products != null) { %>
            
            //Access Product Arrays	    
            
            <% for (int i=0; i<products.productsList.size(); i++) {
            Product currProd = products.productsList.get(i);
            
            //There are are whole bunch of conditions that check the values coming thorugh from the JSP array lists
           
            %>
			
	    	productResults(<%= i %>,
                            "<%= prodId %>",
                            "<%= prodName %>",
                            "<%= prodManufacturer %>",
                            "<%= prodDescription %>",
                            "<%= prodRetailer %>",
                            "<%= prodLogo %>",
                            "<%= prodLogoHeight %>",
                            "<%= prodLogoWidth %>",
                            "<%= lowPriceAmount %>",
                            "<%= lowPriceCurrency %>",
                            "<%= highPriceAmount %>",
                            "<%= highPriceCurrency %>"
                            );
            
            //Access Ratings Arrays
            
            <% for(int j=0; j<currProd.ratingList.size(); j++) { 
            
             //There are are whole bunch of conditions that check the values coming thorugh from the JSP array lists
            
            %>
			
            ratingResults(<%= j %>,
            			  <%= i %>,
                    	  "<%= prodRatType %>",
                    	  "<%= prodRatAvg %>",
                    	  "<%= prodRatNum %>"
                    	  );				
           	<% } %>
            
            //Access Retailer Arrays

            <% for(int x=0; x<currProd.retailerList.size(); x++) {
            
             //There are are whole bunch of conditions that check the values coming thorugh from the JSP array lists
            
            %>
            
            retailerResults(<%= k %>
                            <%= i %>,
                            "<%= currProd.retailerList.get(k).id %>",
                            "<%= currProd.retailerList.get(k).name %>",
                            "<%= currProd.retailerList.get(k).link %>",
                            "<%= retailLogo %>",
                            "<%= currProd.retailerList.get(k).leadTime %>",
                            "<%= currProd.retailerList.get(k).stockInfo %>",
                            "<%= currProd.retailerList.get(k).price.amount %>",
                            "<%= currProd.retailerList.get(k).price.currency %>",
                            "<%= currProd.retailerList.get(k).sourceType %>",
                            "<%= currProd.retailerList.get(k).sourceDate %>",
                            "<%= minShipPrice %>",
                            "<%= minShipCurrency%>",
                            "<%= maxShipPrice %>",
                            "<%= maxShipCurrency %>"
                            );
			<% } %>
            <% } %>
            <% } %>
            }

        var mainResults = [];
	
		function productResults(pnum, pid, pname, pmanu, pdesc, pretail, plogo, plogoh, plogow, plowamt, plowcur, phghamt, phghcur)
		{
			var product = [];
			
			product.count = pnum;
			product.id = pid;
			product.name = pname;
			product.manufacturer = pmanu;
			product.description = pdesc;
			product.numretailer = pretail;
			product.logo = plogo;
			product.logoh = plogoh;
			product.logow = plogow;
			product.lowpriceamt = plowamt;
			product.lowpricecur = plowcur;
			product.highpriceamt = phghamt;
			product.highpricecur = phghcur;
            product.ratings = [];
            product.retailers = [];
			mainResults[pnum] = product;
		}
    
		function ratingResults(ratnum, pnum, rtgtype, rtgavg, rtgnum)
		{
			var ratings = [];
		
			ratings.count = ratnum;
			ratings.type = rtgtype;
			ratings.average = rtgavg;
			ratings.number = rtgnum;
			mainResults[pnum].ratings[ratnum] = ratings;
		}

        function retailerResults(retnum, pnum, rid, rname, rlink, rlogo, rtime, rstock, ramt, rcur, rstype, rsdate, rminamt, rmincur, rmaxamt, rmaxcur)
        {
            var retailers = [];
            
            retailers.count = retnum;
            retailers.id = rid;
            retailers.name = rname;
            retailers.purchaselink = rlink;
            retailers.logo = rlogo;
            retailers.leadtime = rtime;
            retailers.stock = rstock;
            retailers.priceamt = ramt;
            retailers.pricecur = rcur;
            retailers.sourcetype = rstype;
            retailers.sourcedate = rsdate;
            retailers.shipminprice = rminamt;
            retailers.shipmincur = rmincur;
            retailers.shipmaxprice = rmaxamt;
            retailers.shipmaxcur = rmaxcur;
            mainresults[pnum].retailers[retnum] = retailers;
        }


Hi All,

Thank you to all that viewed this thread - I know I probably didnt include enough information for you to work with in the first place but I am happy to say that I have now fixed the issue I was having.

Cheers

Jim