300

I'm trying to check the length here. Tried count. Is there something I'm missing?

var dNames = {}; 
dNames = GetAllNames();

for (var i = 0, l = dName.length; i < l; i++) 
{
        alert("Name: " + dName[i].name);
}

dNames holds name/value pairs. I know that dNames has values in that object but it's still completely skipping over that and when I alert out even dName.length obviously that's not how to do this...so not sure. Looked it up on the web. Could not find anything on this.

3
  • 5
    There's no "length" property on plain objects. That's something that Array instances have. Commented Jul 26, 2010 at 17:42
  • 3
    Ok, anyone know the proper or polite way to award the answer? I know sberry came up with really "the answer" but meder was able to explain it to an intermediate JS guy like myself. I dno't want to piss anyone off. Commented Jul 26, 2010 at 18:31
  • You award the answer to the person who's answer worked for you. Commented Aug 23, 2017 at 0:53

4 Answers 4

716

What I do is use Object.keys() to return a list of all the keys and then get the length of that

Object.keys(dictionary).length
Sign up to request clarification or add additional context in comments.

9 Comments

Just what I needed! Cheers
FYI for the others still supporting IE8: No dice. link
In Chrome 53, Im seeing Object.keys(dictionary) report a length of 1 even though its an empty dictionary. It has an entry called '$type'.
the most used solution and probably the worst one, you have to loop through the whole dictionary to get the length so it's O(n) instead of O(1) like it should be, ugh
Now that javascript has the Map object, probably better off using that and using new Map().size
|
10
var c = {'a':'A', 'b':'B', 'c':'C'};
var count = 0;
for (var i in c) {
   if (c.hasOwnProperty(i)) count++;
}

alert(count);

4 Comments

ok so you have to iterate through...no way to do it off a custom object that has name/values?
I don't think there is. Make sure to check the "hasOwnProperty" to stop iteration down up the prototype chain and only check properties.
Thanks..I'm more an OOP guy, but starting to get used to JS more.
This is the most reliable way to do this in JS. See sberry's note about hasOwnProperty as well. The key, @coffeeaddict, is that a JS object can behave like, but is not fundamentally the same as, a "collection" object in other languages. Hence the puzzling lack of a convenience method for getting the total number of properties on it.
3

This question is confusing. A regular object, {} doesn't have a length property unless you're intending to make your own function constructor which generates custom objects which do have it ( in which case you didn't specify ).

Meaning, you have to get the "length" by a for..in statement on the object, since length is not set, and increment a counter.

I'm confused as to why you need the length. Are you manually setting 0 on the object, or are you relying on custom string keys? eg obj['foo'] = 'bar';. If the latter, again, why the need for length?

Edit #1: Why can't you just do this?

list = [ {name:'john'}, {name:'bob'} ];

Then iterate over list? The length is already set.

4 Comments

So an array has a count property...because probably that for is built into the JS native type?
[] has length, {} doesnt. use a for loop with length for [] and a for..in for {}.
To answer your question yea, it's confusing because I'm not a JS guy, I'm a C# guy. So I needed to check the length of my dictionary to test whether it had any objects in it. For regular error handling I guess you'd just check for null on a dictionary but just wanted to see how many returned and was added to my dictionary. So the problem was not how to iterate, just see how to get the count which I just thought you could with a dictionary type (nvp) object so-to-say.
yes, I'm using obj['foo'] = 'bar'; type of deal to get something that acts as a dictionary.
1

Count and show keys in a dictionary (run in console):

o=[];count=0; for (i in topicNames) { ++count; o.push(count+": "+ i) } o.join("\n")

Sample output:

"1: Phase-out Left-hand
2: Define All Top Level Taxonomies But Processes
3: 987
4: 16:00
5: Identify suppliers"

Simple count function:

function size_dict(d){c=0; for (i in d) ++c; return c}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.