/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] quick object question

Does anyone know if there’s a way to check if an object has no elements?

[CODE]var myObject = new Object;
isEmpty(myObject);[/CODE]

somehow.

I know you could do this:

[CODE]function isEmpty(anObject) {
var count = 0;
for(var elem in anObject) {count++};
if(count > 0) return false;
else return true;
}[/CODE]

But that’s a trifle long-winded. I figured that if when you do for(x in object) it iterates the number of times for the number of elements in the object, there must be a simpler way to get this number….

Anyone have any idea?

Thanks, Robin.

to post a comment
JavaScript

10 Comments(s)

Copy linkTweet thisAlerts:
@rootApr 16.2007 — check its length.

An Object is another sort of Array.
Copy linkTweet thisAlerts:
@rootApr 16.2007 — BTW, what part of Nottingham?
Copy linkTweet thisAlerts:
@theRatWonderauthorApr 16.2007 — Nope, doesn't work. At least not in Firefox 2.0 and therefore I'm assuming it's not W3C.

I'm at the university. I live in uni accomodation - Broadgate Park.
Copy linkTweet thisAlerts:
@theRatWonderauthorApr 16.2007 — And if I may be so bold, an Array is another sort of Object, not the other way around. (Which is why "Object" shouldn't have an inherrent "length" method).
Copy linkTweet thisAlerts:
@rootApr 17.2007 — I'm at the university. I live in uni accomodation - Broadgate Park.[/QUOTE]

Cant place it, is it in the Uni grounds or is that the buildings off Raleigh Island, woolaton road. As for me, I am stuck in NG3.

Anyway, yes, Objects and Arrays...

<i>
</i>count=0;
for(i in Object) count++;

Object is the object you want to iterate through and count, err, counts...
Copy linkTweet thisAlerts:
@theRatWonderauthorApr 17.2007 — Broadgate Park is just off campus - beeston side.. :/ which means I'm even further from town than you are.

The building off raleigh island is Raleigh Park. I should have lived there instead, it's right next to the Jubilee Campus where my studies are.
Copy linkTweet thisAlerts:
@rootApr 17.2007 — Ahh, gotcha. furthest I have been to NU is up lakeside. Your lucky to get into NU, so dont screw up or the rest of yourlife you will be kicking yourself.

I couldnt get in myself, I moved to here from a very nice place but NU wasnt taking any further admissions eventhough I was assured a place, at the last minute my placement was withdrawn, so I ended up spending 2 years on fasttrack at NTU.
Copy linkTweet thisAlerts:
@theRatWonderauthorApr 17.2007 — So I guess the for(i in Object) method is the only way. Ah well, no matter. Thanks.
Copy linkTweet thisAlerts:
@theRatWonderauthorApr 17.2007 — still, NTU's pretty good. U still at it? or have you finished?
Copy linkTweet thisAlerts:
@rootApr 17.2007 — Its been a few years now.

I dont think moving to NG3 helped me, been trying to get out of this toilet for the past couple of years. Its not as bad as it used to be, the number of shootings have plumeted but hoodies are on the rise again.

So your better off out of the city!
×

Success!

Help @theRatWonder 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...