Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #26973 > unrolled thread

How do I access nested key/value pairs?

Started byMasoom Tulsiani <masoom.tulsiani@gmail.com>
First post2015-03-04 07:51 -0800
Last post2015-03-04 12:17 -0800
Articles 9 — 5 participants

Back to article view | Back to comp.lang.javascript


Contents

  How do I access nested key/value pairs? Masoom Tulsiani <masoom.tulsiani@gmail.com> - 2015-03-04 07:51 -0800
    Re: How do I access nested key/value pairs? John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-04 16:14 +0000
    Re: How do I access nested key/value pairs? Denis McMahon <denismfmcmahon@gmail.com> - 2015-03-04 18:08 +0000
      Re: How do I access nested key/value pairs? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-05 16:25 +0100
        Re: How do I access nested key/value pairs? John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-06 10:26 +0000
          Re: How do I access nested key/value pairs? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-03-06 12:20 +0100
            Re: How do I access nested key/value pairs? John Harris <niam@jghnorth.org.uk.invalid> - 2015-03-06 20:18 +0000
    Re: How do I access nested key/value pairs? Scott Sauyet <scott.sauyet@gmail.com> - 2015-03-04 12:15 -0800
      Re: How do I access nested key/value pairs? Scott Sauyet <scott.sauyet@gmail.com> - 2015-03-04 12:17 -0800

#26973 — How do I access nested key/value pairs?

FromMasoom Tulsiani <masoom.tulsiani@gmail.com>
Date2015-03-04 07:51 -0800
SubjectHow do I access nested key/value pairs?
Message-ID<3e102a71-73a4-41d5-811d-de9d0952f0cd@googlegroups.com>
I have the following hash structure with nested key/value pairs. 

How can I access a particular key and value pair. For Instance,I would like to access  FIELDS -> UNAME(key)
and FIELDS -> STRING (value) of the message with OBJ_TYPE: FEED 


var hash =

{

{ OBJ_TYPE: 'RESULT',    
   NAME: 'Result.ResultName,
   FIELDS:
    { UNAME: 'STRING',
       DESC: 'STRING',
    },
 }

{ OBJ_TYPE: 'FEED', // Key is Obj_type and value is Feed
   NAME: 'Feed.FeedName,  // Key is name and value is Feed.FeedName
    FIELDS:
    { UNAME: 'STRING',
       DESC: 'STRING',
    },
 }

{ OBJ_TYPE: 'X',
   NAME: 'X.XName,
    FIELDS:
    { UNAME: 'STRING',
       DESC: 'STRING',
    },
 }

}



App.js
 
        		          var hash = { /*structure that I described above */};

                                   for(var k in hash)
        			 {
        			 if(hash.hasOwnProperty(k) && k=="NAME" && hash[k]=="Feed.FeedName")
        				 {
        				         				 
        				 // I could do something like var fields = hash[k]=="FIELDS";
        				 for(var eachField in fields){
    						 console.log(eachField);
    						    						 
    					 }
                                 }

[toc] | [next] | [standalone]


#26974

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2015-03-04 16:14 +0000
Message-ID<robefa9ildb7hl9rnt59mkgd153cor7p5i@4ax.com>
In reply to#26973
On Wed, 4 Mar 2015 07:51:44 -0800 (PST), Masoom Tulsiani
<masoom.tulsiani@gmail.com> wrote:

>I have the following hash structure with nested key/value pairs. 
>
>How can I access a particular key and value pair. For Instance,I would like to access  FIELDS -> UNAME(key)
>and FIELDS -> STRING (value) of the message with OBJ_TYPE: FEED 
>
>
>var hash =
>
>{
>
>{ OBJ_TYPE: 'RESULT',    
>   NAME: 'Result.ResultName,
>   FIELDS:
>    { UNAME: 'STRING',
>       DESC: 'STRING',
>    },
> }
>
>{ OBJ_TYPE: 'FEED', // Key is Obj_type and value is Feed
>   NAME: 'Feed.FeedName,  // Key is name and value is Feed.FeedName
>    FIELDS:
>    { UNAME: 'STRING',
>       DESC: 'STRING',
>    },
> }
>
>{ OBJ_TYPE: 'X',
>   NAME: 'X.XName,
>    FIELDS:
>    { UNAME: 'STRING',
>       DESC: 'STRING',
>    },
> }
>
>}
>
>
>
>App.js
> 
>        		          var hash = { /*structure that I described above */};
>
>                                   for(var k in hash)
>        			 {
>        			 if(hash.hasOwnProperty(k) && k=="NAME" && hash[k]=="Feed.FeedName")
>        				 {
>        				         				 
>        				 // I could do something like var fields = hash[k]=="FIELDS";
>        				 for(var eachField in fields){
>    						 console.log(eachField);
>    						    						 
>    					 }
>                                 }

Couldn't an array be used to hold the key/data objects ? It's
certainly easier to scan.

  John

[toc] | [prev] | [next] | [standalone]


#26978

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2015-03-04 18:08 +0000
Message-ID<md7hnr$3qu$1@dont-email.me>
In reply to#26973
On Wed, 04 Mar 2015 07:51:44 -0800, Masoom Tulsiani wrote:

> var hash =

hash seems to be an object containing three anonymous objects with no 
means of distinguishing them.

Trying to load this in javascript causes an error because each element of 
an object (including a nested object) requires an identifier.

Perhaps you mean hash to be an array?

You also have some unterminated string literals.

Once I fixed hash up to be a syntactically correct array of obs, the 
following code worked:

for (i=0; i<hash.length; i++) {
    if (hash[i].NAME) {
        console.log('hash['+i+'].NAME = '+hash[i].NAME);
        if (hash[i].NAME=='Feed.FeedName')
            for (p in hash[i].FIELDS)
                if (hash[i].FIELDS.hasOwnProperty(p))
                    console.log('hash['+i+'].FIELDS.'+p+' = '+
                                hash[i].FIELDS[p]);
    }
}


-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#26992

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-03-05 16:25 +0100
Message-ID<2031858.aWiVlKuHNg@PointedEars.de>
In reply to#26978
Denis McMahon wrote:

> On Wed, 04 Mar 2015 07:51:44 -0800, Masoom Tulsiani wrote:
>> var hash =
> 
> hash seems to be an object containing three anonymous objects with no
> means of distinguishing them.
> 
> Trying to load this in javascript causes an error because each element of
> an object (including a nested object) requires an identifier.

Utter nonsense.

- There is no “javascript”, see my sig.
- Objects do not have names (so it makes no sense to speak of “anonymous 
  objects”); they have identity (one object is different from another).
- Objects do not have elements; they have properties.  *Arrays* have
  elements; they are objects which also have indexes (special property
  names).
- Property names do not need to be identifiers (for example, indexes are
  not identifiers as they are representation of integers).  In
  implementations of ECMAScript Edition 5 and later they can even be
  reserved words.

  var x = {"1701foo": "bar"};
  var u = x;

  /* "bar" */
  u["1701foo"]

  x["1701foo"] = "baz";

  /* "baz" */
  u["1701foo"]

  /* true */
  u == x

  /* true */
  u === x

  /* ES 5+ only */
  x.float = Math.PI;

  /* 3.141592653589793… */
  u.float

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#26995

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2015-03-06 10:26 +0000
Message-ID<o60jfa9fs6hn6gcllp5ple48501r8h8ni8@4ax.com>
In reply to#26992
On Thu, 05 Mar 2015 16:25:27 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>Denis McMahon wrote:
  <snip>
>> Trying to load this in javascript causes an error because each element of
>> an object (including a nested object) requires an identifier.
>
>Utter nonsense.
>
>- There is no “javascript”,
  <snip>

A minority opinion.

  John

[toc] | [prev] | [next] | [standalone]


#26996

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2015-03-06 12:20 +0100
Message-ID<4181479.JrGZDH12zE@PointedEars.de>
In reply to#26995
John Harris wrote:

> […] Thomas 'PointedEars' Lahn […] wrote:
>> Denis McMahon wrote:
>   <snip>
>>> Trying to load this in javascript causes an error because each element
>>> of an object (including a nested object) requires an identifier.
>>
>> Utter nonsense.
>>
>>- There is no “javascript”,
>   <snip>
> 
> A minority opinion.

<http://rationalwiki.org/wiki/Argumentum_ad_populum>

-- 
PointedEars
FAQ: <http://PointedEars.de/faq> | SVN: <http://PointedEars.de/wsvn/>
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-matrix>
Please do not cc me. / Bitte keine Kopien per E-Mail.

[toc] | [prev] | [next] | [standalone]


#27001

FromJohn Harris <niam@jghnorth.org.uk.invalid>
Date2015-03-06 20:18 +0000
Message-ID<8o2kfahp0bd0scfu86e4nui8h22a2cs7os@4ax.com>
In reply to#26996
On Fri, 06 Mar 2015 12:20:09 +0100, Thomas 'PointedEars' Lahn
<PointedEars@web.de> wrote:

>John Harris wrote:
>
>> […] Thomas 'PointedEars' Lahn […] wrote:
>>> Denis McMahon wrote:
>>   <snip>
>>>> Trying to load this in javascript causes an error because each element
>>>> of an object (including a nested object) requires an identifier.
>>>
>>> Utter nonsense.
>>>
>>>- There is no ?javascript?,
>>   <snip>
>> 
>> A minority opinion.
>
><http://rationalwiki.org/wiki/Argumentum_ad_populum>

Thomas can't have read that article.

It doesn't say that the Flat Earth Society is guaranteed to be right.

It specifically excludes human languages - grammar and the meaning of
words.

So, it would apply to "There is only one language". It would not apply
to "It is called X-LANG".

  John

[toc] | [prev] | [next] | [standalone]


#26985

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-03-04 12:15 -0800
Message-ID<7e1f3540-b4db-4ae6-910c-3b7f2c94e986@googlegroups.com>
In reply to#26973
Masoom Tulsiani wrote:
> I have the following hash structure with nested key/value pairs. 

As Denis mentioned, your structure took some rework before it could be comfortably loaded in JS.  This was my version:

    var hash = [
      { 
        OBJ_TYPE: 'RESULT',     
        NAME: 'Result.ResultName', 
        FIELDS: [
          {UNAME: 'STRING', DESC: 'STRING'}
        ]
      },
      { 
        OBJ_TYPE: 'FEED',
        NAME: 'Feed.FeedName',
        FIELDS: [ 
          {UNAME: 'STRING', DESC: 'STRING'}
        ] 
       },
      { 
        OBJ_TYPE: 'X', 
        NAME: 'X.XName', 
        FIELDS: [ 
          {UNAME: 'STRING', DESC: 'STRING'}
        ]
      } 
    ]; 

> How can I access a particular key and value pair. For Instance,I would 
> like to access  FIELDS -> UNAME(key) and FIELDS -> STRING (value) of 
> the message with OBJ_TYPE: FEED 

This function applies a callback function to each matching field:

    var processFields = function(hash, type, name, fn) {
      hash.filter(function(obj) {
        return obj.OBJ_TYPE === type && obj.NAME === name;
      }).forEach(function(obj) {
        obj.FIELDS.forEach(function(field) {fn(field);});
      });
    };
    
    processFields(hash, 'FEED', 'Feed.FeedName', function(field) {
      console.log(field);
    }); // logs {UNAME: "STRING", DESC: "STRING"}

It could easily be modified to collect and return the fields instead of
applying a callback, though.

HTH,

  -- Scott

[toc] | [prev] | [next] | [standalone]


#26986

FromScott Sauyet <scott.sauyet@gmail.com>
Date2015-03-04 12:17 -0800
Message-ID<05904496-1d69-419f-90bb-44ebae6a9753@googlegroups.com>
In reply to#26985
Scott Sauyet wrote:
> This function applies a callback function to each matching field:
> 
>     var processFields = function(hash, type, name, fn) {
>       hash.filter(function(obj) {
>         return obj.OBJ_TYPE === type && obj.NAME === name;
>       }).forEach(function(obj) {
>         obj.FIELDS.forEach(function(field) {fn(field);});
>       });
>     };

I should have noted that it does not do some of the error-checking
that you probably should do.  It would fail if `obj.FIELDS` didn't
exist.

It also will not work in some older environments without a shim, as
it depends on features of `Array.prototype` not standardized until
ES5.

  -- Scott

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web