www.webdeveloper.com
Results 1 to 14 of 14

Thread: Want to reuse for loop but change condition from within loop

  1. #1
    Join Date
    Sep 2004
    Posts
    15

    Want to reuse for loop but change condition from within loop

    Hi,

    I have an object and need to set groups of values based on the condition. My loop looks like

    for(s=0;s<product.system.length;s++){
    xfa.resolveNode("form1.#pageSet.Page1.Subform1."+system+".Row["+s+"].Cell[0]").rawValue = product.system[s][0];
    xfa.resolveNode("form1.#pageSet.Page1.Subform1."+system+".Row["+s+"].Cell[1]").rawValue = product.system[s][1];
    xfa.resolveNode("form1.#pageSet.Page1.Subform1."+system+".Row["+s+"].Cell[3]").rawValue = product.system[s][2];
    xfa.resolveNode("form1.#pageSet.Page1.Subform1."+system+".Row").instanceManager.addInstance(0);

    }

    So after I hit the end of product.system.length, I want to sub in another word for "system" and go through the loop again until I've hit the end of that group.

    Product is an object and it has properties product.system, product.reagents, product.controls, and product.accessories.

    I can do this if I make 4 separate for loops, but surely there is a cleaner way?

  2. #2
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Well, if you're looping through all the members of the product object, you could do this:

    Code:
    <script type="text/javascript">
    
    	var
    		product = {
    			system      : [1],
    			reagents    : [1, 2],
    			controls    : [1, 2, 3],
    			accessories : [1, 2, 3, 4]
    		},
    		i, s;
    
    	for (i in product) {if (product.hasOwnProperty(i)) {
    		for (s = 0; s < product[i].length; ++s) {
    
    			alert(i + ' - ' + product[i][s]);
    			//YOUR CODE
    
    		}
    	}}
    
    </script>
    If you have other members you don't want to iterate through then we can supply a custom list instead.
    Last edited by bionoid; 12-26-2013 at 02:02 PM.

  3. #3
    Join Date
    Sep 2004
    Posts
    15
    yes I'll want to fill in separate tables with each group (member?) of the product object, so yes, I will hit them all. Your code is advanced and I don't quite understand it, but I'll do some reading on hasOwnProperty and come back to this post.

    when you wrote this
    var
    product = {
    system : [1],
    reagents : [1, 2],
    controls : [1, 2, 3],
    accessories : [1, 2, 3, 4]
    },

    You're just declaring the object right?

  4. #4
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Quote Originally Posted by garek007 View Post
    when you wrote this
    var
    product = {
    system : [1],
    reagents : [1, 2],
    controls : [1, 2, 3],
    accessories : [1, 2, 3, 4]
    },

    You're just declaring the object right?
    Yes, that was just a dummy object to show the routine displayed all values in each object member.

  5. #5
    Join Date
    Sep 2004
    Posts
    15
    bionoid,

    Thank you. This seems to do what I want, but I also want to understand it more. For in loops still confuse me.... Can you walk me through what is happening here?

  6. #6
    Join Date
    Sep 2004
    Posts
    15
    also, do I really NEED hasOwnProperty? It seems like this would work without it since this is a custom declared object that isn't inheriting anything.

  7. #7
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Code:
    for ( i in product ) {
    FOR IN is useful for iterating through unsequential data, typically found in objects with named members.
    "i" with become the index of each member as it loops, eg: system, reagents, controls, accessories.

    Code:
    if (product.hasOwnProperty(i)) {
    hasOwnProperty was used to filter out members that the object inherited, normally defined through prototype.
    Last edited by bionoid; 12-26-2013 at 03:50 PM.

  8. #8
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Quote Originally Posted by garek007 View Post
    also, do I really NEED hasOwnProperty? It seems like this would work without it since this is a custom declared object that isn't inheriting anything.
    It's a precautionary measure, and it's recommended by the validator I like to use. www.jslint.com

  9. #9
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    Quote Originally Posted by garek007 View Post
    also, do I really NEED hasOwnProperty? It seems like this would work without it since this is a custom declared object that isn't inheriting anything.
    All objects inherits properties from their prototype object. Objects decleared with the object initializer expression ( {} ) use Object object as their prototype object.

  10. #10
    Join Date
    Sep 2004
    Posts
    15
    ok I was able to get it to work without hasOwnProperty, but you're saying I should still use it right? as a precaution? Thanks, this worked great!

  11. #11
    Join Date
    Dec 2011
    Location
    Centurion, South Africa
    Posts
    795
    Some frameworks that you might be inclined to use in the future could be extending the Object object, which is of course considered bad practise, but nothing stops them doing it. Then your innocent looking loop might return unexpected results.

  12. #12
    Join Date
    Sep 2004
    Posts
    15
    good to know, thanks bionoid!

  13. #13
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,498
    If using an array, possibly an array of objects will be better, eg.

    Code:
    product = [ 
        {system : [1], reagents : [1, 2], controls : [1, 2, 3], accessories : [1, 2, 3, 4] },
        {system : [1], reagents : [1, 2], controls : [1, 2, 3], accessories : [1, 2, 3, 4] },
        {system : [1], reagents : [1, 2], controls : [1, 2, 3], accessories : [1, 2, 3, 4] },
        {system : [1], reagents : [1, 2], controls : [1, 2, 3], accessories : [1, 2, 3, 4] }
    ];
    then your Array elements are accessed via a loop as well but the object properties are then accessible, eg..

    product[i].system
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  14. #14
    Join Date
    Sep 2004
    Posts
    15
    thanks \\.\, I appreciate the help... but I did already get it working with the object so all is good!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles