0

Trying to get the length of a variable territories, yet when I do territories.length it gives me 0. When I use console.log and look in the JavaScript console, it shows

child 
{
    length: 0, 
    models: Array[0], 
    _byId: Object, 
    _events: Object, 
    _listenerId: "l11"
}

So the length is 0 but when I expand that, it says that the length is9. I want it to be 9. How can I get length:9?

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
  • 1
    What's `territories`, can you give us a bit more details ? – Dimitar Dimitrov Jan 05 '14 at 02:45
  • 1
    In your example the length is indeed 0, when you say "when I expand that, it says that the length is 9" what do you mean ? What are you expanding ? – Dimitar Dimitrov Jan 05 '14 at 02:50
  • @DimitarDimitrov In the JavaScript console. See [this.](http://i.imgur.com/kjSOPXB.png) – user3161730 Jan 05 '14 at 02:52
  • 1
    Could you post the entire code you need to get your `child`? – scrblnrd3 Jan 05 '14 at 02:55
  • @scrblnrd3 It has to do with BackboneJS. This is how `territories` is defined: `var Territories = Backbone.Collection.extend` – user3161730 Jan 05 '14 at 02:57
  • Please make a [jsFiddle](http://jsFiddle.net) for us – scrblnrd3 Jan 05 '14 at 02:58
  • @scrblnrd3 is there a way to find the number of models? – user3161730 Jan 05 '14 at 03:01
  • @user3161730 In the example you've provided `models` is an array with the size of 0. How about you prepare a fiddle and let us take a look. – Dimitar Dimitrov Jan 05 '14 at 03:06
  • The problem you're having in your console is because your object is later by another script modified to length:9. This makes it show length:9 in your console, while it was still 0 when the script was running. To get a true capture of what the actual value of the object was, you can use `JSON.stringify(obj)`. That will capture the object at that moment, and show the true values of the object. – Joeytje50 Jan 05 '14 at 03:52

2 Answers2

1

You can do this to get the number of properties in any javascript object, at least in modern browsers:

Object.keys(myObj).length;

You could also do this manually, like this

Object.prototype.length=function(){
    var count=0;
    for(var i in this){
        if(this.hasOwnProperty(i)){
            count++;
        }
    }
    return count;
}
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
0

Length is the right property, but it depends on what kind of variable territories is. The below code for a string example outputs 9.

var territories = "my string";
var length = territories.length;
alert(length);

Since in this case territories is actually a backbone collection, you can find more information about this issue from the following question: Get collection length

Unfortunately you can't save the fetched collection data because the fetch function in backbone is asynchronous. You need to use a callback function to do whatever you need done with the length property. For more information see How to return value from an asynchronous callback function? and http://recurial.com/programming/understanding-callback-functions-in-javascript/

Community
  • 1
  • 1
Andrew L
  • 1,164
  • 12
  • 20
  • `territories` is not a string. – user3161730 Jan 05 '14 at 02:58
  • I see, it looks like this question may have already answered it for you: http://stackoverflow.com/questions/14527755/get-collection-length – Andrew L Jan 05 '14 at 03:07
  • How would I save this as a variable though? – user3161730 Jan 05 '14 at 03:21
  • It's not working anymore. When I use `console.log(x)` it gives me `Uncaught TypeError: object is not a function` – user3161730 Jan 05 '14 at 03:54
  • Do you understand why? – user3161730 Jan 05 '14 at 04:28
  • I think it's because fetching the collection is an asynchronous action and you are trying to assign the variable before the call back has finished. This is a fairly complicated topic, so I would suggest posting a new question to get more responses. I'll edit my answer one more time and you can see if that works. – Andrew L Jan 05 '14 at 04:33