[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

How do I access nested key/value pairs?

Masoom Tulsiani

3/4/2015 3:52:00 PM

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);

}
}
10 Answers

iL_WeReo

8/2/2010 12:07:00 AM

0

On Aug 1, 7:57 pm, Seth Buttock <calo...@earthlink.net> wrote:
> On Aug 1, 10:20 am, "Sweetbac" <sweetb...@scbglobal.net> wrote:
>
> > Well at least I know what I WONT be having for dinner.
>
> Most nights, it's a pretty short list!
>
> Right, screen name?

I eat like a king.

John Harris

3/4/2015 4:14:00 PM

0

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

Denis McMahon

3/4/2015 6:09:00 PM

0

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

Scott Sauyet

3/4/2015 8:15:00 PM

0

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

Scott Sauyet

3/4/2015 8:18:00 PM

0

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

Thomas 'PointedEars' Lahn

3/5/2015 3:25:00 PM

0

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 == o

/* true */
u === o

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

/* 3.141592653589793â?¦ */
u.float

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

Thomas 'PointedEars' Lahn

3/5/2015 3:25:00 PM

0

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.... | SVN: <http://PointedEars.de...
Twitter: @PointedEars2 | ES Matrix: <http://PointedEars.de/es-...
Please do not cc me. / Bitte keine Kopien per E-Mail.

John Harris

3/6/2015 10:27:00 AM

0

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

Thomas 'PointedEars' Lahn

3/6/2015 11:20:00 AM

0

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_p...

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

John Harris

3/6/2015 8:18:00 PM

0

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_p...

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