On Sat, Jun 25, 2011 at 9:26 PM, Matthew Bramer <[email protected]> wrote:
> I think that subject title is syntactically correct.  Feel free to correct
> me if I'm mistaken.
> Here's some sample code:
> console.log( somePlugin.Query({
> listName: "Bid Key",
> config: {
> 1: {
> staticName: "ModuleNotes",
> value: "Some Comments"
> },
> 2: {
> staticName: "ID",
> value: 14,
> LookupId: true
> }
> }
>    }
>  })
> );
> What I'm trying to do is figure out how many things are stuffed inside of
> config.  In this example it would be 2, but there could be 20.  Based on the
> number of things stuffed into config, I need to run different code.

If you are in an environment where you can guarantee ECMAScript 5 (eg.
within Node.js), you can do:

    Object.keys( opt.config ).length

However, you are likely executing within a browser.  In which case,
you can't assume you have ES5 and should iterate through the
properties, finding just those that explicitly belong to this object:

    var num = 0;
    for ( prop in opt.config ) {
        if ( opt.config.hasOwnProperty( prop ) ) num++;
    }
    console.log( num );

Of course, a better solution may be to shim in the Object.keys method:

    if ( Object.keys === undefined ) {
        Object.keys = function( obj ) {
            var keys = [];
            for ( prop in obj ) {
                if ( obj.hasOwnProperty( prop ) ) keys.push( prop );
            }
            return keys;
        }
    }

and use it as in the ES5 solution.

_jason

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to