Results 1 to 4 of 4

Thread: I think I'm really getting this JS stuff!!

  1. #1

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    I think I'm really getting this JS stuff!!

    I had this

    Code:
    objGrid.source = [];
    for (var i = 0; i < arrSource.length; i++) {
        var objNew = {};
        objNew.Fund = arrSource[i].label.substring(0, 2);
        objNew.Function = arrSource[i].label.substring(3, 7);
        objNew.Object = arrSource[i].label.substring(8, 11);
        objNew.FundSrc = arrSource[i].label.substring(12, 15);
        objNew.Grade = arrSource[i].label.substring(16, 18);
        objNew.Bldg = arrSource[i].label.substring(19, 21);
        objNew.Dept = arrSource[i].label.substring(22, 25);
        objNew.Misc = arrSource[i].label.substring(26, 30);
        objNew.SLASN = arrSource[i].value;
        objGrid.source.push(objNew);
    }
    When I had the VAR OBJNEW = {} above and outside of the loop it was putting 4000 of the "same" object into the array (the last object pushed became all 4000 entries).

    Then after reading the "very obvious" statement that putting a VAR declaration into a for/loop/block didn't create new scope...

    I started thinking "What am I really doing here" and "what do I really want to do".

    So I changed it to this...

    Code:
    objGrid.source = [];
    for (var i = 0; i < arrSource.length; i++) {
        objGrid.source.push({
                Fund: arrSource[i].label.substring(0, 2),
                Function: arrSource[i].label.substring(3, 7),
                Object: arrSource[i].label.substring(8, 11),
                FundSrc: arrSource[i].label.substring(12, 15),
                Grade: arrSource[i].label.substring(16, 18),
                Bldg: arrSource[i].label.substring(19, 21),
                Dept: arrSource[i].label.substring(22, 25),
                Misc: arrSource[i].label.substring(26, 30),
                SLASN: arrSource[i].value})
    }
    I realized that I didn't need to create an "object" at all just because I wanted to push an OBJECT representation into an array.

    I could do it "literally" by putting an OBJECT TYPE into the PUSH() method.

    I'm not creating 4000 objects in a loop - I'm cleanly pushing 4000 object entries into an array.

    Am I making sense? Am I accurately stating these facts??

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  2. #2
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: I think I'm really getting this JS stuff!!

    Yes, you are making sense.
    In fact, the two code samples you posted do the same thing. I agree with you that the second is superior. The temporary variable in the first is unnecessary, and the object notation of the second is more efficient, because you're creating and initialising the object in one command.

    I might make one further improvement:
    Code:
    objGrid.source = arrSource.map(function(obj) {
    	return {
    		Fund: obj.label.substring(0, 2),
    		Function: obj.label.substring(3, 7),
    		Object: obj.label.substring(8, 11),
    		FundSrc: obj.label.substring(12, 15),
    		Grade: obj.label.substring(16, 18),
    		Bldg: obj.label.substring(19, 21),
    		Dept: obj.label.substring(22, 25),
    		Misc: obj.label.substring(26, 30),
    		SLASN: obj.value
    	};
    });
    This is a trick borrowed from functional programming.
    The map function produces a new array from a source array. The callback function which you pass in takes each element of the source array and returns a new element which goes into the new array.
    This approach has two benefits:
    — the target array is initialised with the same size as the original array, whilst push resizes the array every time;
    — brevity.

    In general , whenever I see a for(;;) loop in JavaScript, I think to myself whether it can be replaced with a foreach, for...in, or a function such as map, reduce, filter, or every.
    Last edited by penagate; May 9th, 2011 at 08:15 PM.

  3. #3

    Thread Starter
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: I think I'm really getting this JS stuff!!

    First - I gotta say - the use of callback functions is new to me - but I'm starting to see that the size of my JS file can be really shrunken if I consider passing "callback functions" to functions so my code reuse ratio really climbs.

    Quote Originally Posted by penagate View Post
    ...In general , whenever I see a for( ; ; ) loop in JavaScript, I think to myself whether it can be replaced with a foreach, for...in, or a function such as map, reduce, filter, or every.
    But more important - wow - the way you just described using "set-based" methods to process blocks of data is almost 4NF - that unattainable form that us SQL programmers try to raise our resultsets to.

    I just stumbled upon some pictures I took back in 2004 that had an XML-video-lession plan sitting on a shelf (like 4 vhs tapes and a 3 inch thick workbook - what the hell could they teach you about XML in 4 hour-long tapes!)

    I never wasted the four hours - glad to say I've still got them in my queue...

    JSON effortlessly launches my 3NF data into the "logic" area where 4NF takes over - and JS seems to actually be able to work it in 4NF form.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: I think I'm really getting this JS stuff!!

    Quote Originally Posted by szlamany View Post
    When I had the VAR OBJNEW = {} above and outside of the loop it was putting 4000 of the "same" object into the array (the last object pushed became all 4000 entries).
    I just read this again. This shouldn't be the case. On each iteration, objNew will point to a new object. I can't see how you can end up with duplicate objects.

    Edit: Just read it again... I see that you had it outside the loop... D'oh!


    Quote Originally Posted by szlamany
    I just stumbled upon some pictures I took back in 2004 that had an XML-video-lession plan sitting on a shelf (like 4 vhs tapes and a 3 inch thick workbook - what the hell could they teach you about XML in 4 hour-long tapes!)
    Ouch...
    I like to think that XML can be taught in a matter of minutes. Most of it is a waste of time.
    Last edited by penagate; May 9th, 2011 at 11:02 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width