/    Sign up×
Community /Pin to ProfileBookmark

another way to replace for(var key in o) ?

Is code of [B]for(var key in o)[/B] only way to enumerate properties of object o?

Is there [B]second way[/B] for enumerating properties of object o?

Thanks.

.

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@MarPloJun 09.2012 — Hi,

There is also [b]for each (variable in object)[/b].

It works the same way as "for .. in" , but instead of iterating over an objects property names, it iterates over the property values.

Example:
<script type="text/javascript"><!--
var obj = {apple:100, bool:false, astring:"www.coursesweb.net"};
for each (item in obj) {
document.write(item + '<br />');
}
--></script>

Result:
78
false
www.coursesweb.net
Copy linkTweet thisAlerts:
@rnd_meJun 10.2012 — [CODE]var o={a:1,b:2,c:3};

function dump(key, index){
return key + "= " +o[key];
}

alert(
Object.keys(o).map(dump).join("n")
);[/CODE]
Copy linkTweet thisAlerts:
@rnd_meJun 10.2012 — 
There is also [b]for each (variable in object)[/b][/QUOTE]


according to who?
×

Success!

Help @html20009876 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.17,
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,
)...