/    Sign up×
Community /Pin to ProfileBookmark

accessing arrays within JSON

Hello all,
So far I have:

code:

[CODE] $(“#submit”).click(function(){

var validate =
{
“validateRules”:
[
{
“fieldId”: “registration”,
“errMsg”: errNumeric,
“errDiv”: “#regmsg”,
“pattern”: regexNumeric
},
{
“errDiv”: “#regmsg”
},
{
“errDiv”: “#speciesmsg”
},
{
“errDiv”: “#gendermsg”
},
{
“errDiv”: “#bdaymsg”
},
{
“errDiv”: “#aliasmsg”
}
]

};

alert(validate.ValidateRules[0].errDiv);

});[/CODE]

I can access arrays within that JSON code by:

[CODE]validate.ValidateRules[0].errDiv[/CODE]

How do I loop through validate and if errDiv is found grab it’s value and do something….

-Rich

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Declan1991Jan 28.2010 — Using a loop.for (var i = 0, v = validate.validateRules, l = v.length; i < l; i++) {
if (v[i].errDiv) alert("");
}
Copy linkTweet thisAlerts:
@WebnerdJan 29.2010 — <i>
</i>for (i=0;i&lt;validate.validateRules.length;i++)
{
var obj = validate.validateRules[i];
if(obj.errDiv)
{
// do something
}
}
Copy linkTweet thisAlerts:
@dottedquadauthorJan 29.2010 — Thanks Declan1991 and webnerb. Both worked as expected :-).
×

Success!

Help @dottedquad spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...