Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / January 2007



Tip: Looking for answers? Try searching our database.

JSON Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Curtis.DanielN@gmail.com - 30 Jan 2007 15:14 GMT
If you have an object list ex:
var olist = {"daniel":4, "Tom":5};

Is there any way to reference the objects fields w/o knowing their
names? Say for example I pass the list to another function and want to
verify the field names are actually Daniel and Tom or I would like to
count the number of fields(also sorry if fields are not the correct
term). I've tried looking on google but perhaps I'm not using the
correct search terms.

Thanks
Martin Honnen - 30 Jan 2007 15:22 GMT
> If you have an object list ex:
> var olist = {"daniel":4, "Tom":5};
>
> Is there any way to reference the objects fields w/o knowing their
> names?

You can enumerate the enumerable properties with a for..in loop e.g.
  for (var propertyName in olist) {
    alert(propertyName + ': ' + olist[property]);
  }

Signature

    Martin Honnen
    http://JavaScript.FAQTs.com/

Bart Lateur - 30 Jan 2007 21:04 GMT
>If you have an object list ex:
>var olist = {"daniel":4, "Tom":5};
[quoted text clipped - 5 lines]
>term). I've tried looking on google but perhaps I'm not using the
>correct search terms.

You could use a hash, sorry, "object", to find out how two objects' keys
overlap.

    var list1 = {"daniel":4, "Tom":5, "one": 1};
    var list2 = {"daniel":6, "Tom":7, "two": 2};
    var overlap = {};
    function insert (list, bitmask) {
       var p;
       for (p in list) {
           if(!overlap[p]) overlap[p] = 0;
           overlap[p] = overlap[p] | bitmask;
       }
    }
    insert(list1, 1);
    insert(list2, 2);
    // insert(list3, 4);   // powers of 2

Now, what does it do? It creates an object with all keys used in any
object in the input, with a bitmask flag value indicating where it's
found: 1 if only in list1, 2 if only in list2, 3 if in both.

Likewise, you can add a third list, with bitmask 4, a fourth with bitmak
8... and get different integers depending on what lists the items are
in.

So you could just check that all entries in overlap have an associated
value 3.

(n.b. an item that isn't in any input list, won't appear in the
resulting object "overlap" either.)

Signature

    Bart.

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.