-3

In an ajax call i retrieve a JSON object and count the number of result throught lenght property. But in another code, with same kind of call and little modifications to the server-side script, the length propertu retrieve me alway undefined. Why? Note that in the developer console the msg is treated like an object (i tink, converted automatically from JSON by ajax), not such an array.

$.ajax({
  url : 'richiediListaVideo.php',
  type : 'POST',
  data :  data,
  dataType : 'json',
  success : function (msg) 
            {
              alert(msg['videos'].length)


            },

The object whose "undefined" length is something like

-video
--title
--duration
---tags
---8  "funny"
---1352  "animals"
---13    "Tv"

My goal is to retrieve the tags length, and i wrote msg['video']['tags'].length

This is the stringfied version of "msg"

{"video":{"duration":"5:51","views":"2650","video_id":"512498","rating":"5.00","ratings":"5","title":"Lovely dog", "publish_date":"2013-08-05 16:50:08","tags":{"8":"funny", "54":"lol","75":"Animals","89":"Garden"}}}

Clarification: Anyway i know how count the number of tags, but the point is that i really want to know why happen this


Anyway i know how count the number of tags

var length=0; 
for(var elemy in res['video']['tags']) length++

but the point is that i really want to know why happen this

Jarrod Dixon
  • 15,346
  • 9
  • 57
  • 72
Sonia
  • 253
  • 1
  • 3
  • 15
  • what the `console.log(JSON.stringify(msg))` is showing? – Brian Aug 07 '13 at 01:42
  • Unless your msg['videos'] an array(/collection) or a string, it wouldn't have any length. What does your msg['videos'] contain? – Prash Aug 07 '13 at 01:42
  • We have no way to know why is returning `undefined` with provided information. You should do some debugging and come with more :) – Claudio Redi Aug 07 '13 at 01:42
  • What about video? What does it look like? – Brian Aug 07 '13 at 01:46
  • If it's not an array (or a string) it'll only have a `.length` property if you explicitly create/set the property. Please show the actual JSON content, or the `msg` object as displayed by `console.log()`. – nnnnnn Aug 07 '13 at 01:47
  • @A.S. Roma console tells me that some script had disabled the console.log methods.. I tink it's done from the code that returns me results, i haven't disabled anything – Sonia Aug 07 '13 at 01:49
  • Try alerting the same thing? – Brian Aug 07 '13 at 01:52
  • @A.S. Roma, yes, i've updated the answer – Sonia Aug 07 '13 at 01:56
  • possible duplicate of [How to efficiently count the number of keys/properties of an object in JavaScript?](http://stackoverflow.com/questions/126100/how-to-efficiently-count-the-number-of-keys-properties-of-an-object-in-javascrip) – Bergi Aug 07 '13 at 02:01
  • @Bergi it's not a duplicate becouse i not asking how to count (i know how) but WHY happen this – Sonia Aug 07 '13 at 02:03
  • @AnnaLica, this is a basic concept in object-oriented languages. If you need to know the number of items, it probably should be an array. – Brigand Aug 07 '13 at 02:07
  • @FakeRainBrigand ok, but the point is that in another piece of code the same syntax works, though it's not an array – Sonia Aug 07 '13 at 02:13
  • Does it have a length attribute? For example, `{foo: "bar", length: 42}` has a length property. Please paste that JSON as well. – Brigand Aug 07 '13 at 02:15
  • @AnnaLica: If you know how to do it you probably have understood objects. They are not associative arrays, but tuples or records. They just don't have a `length` count. – Bergi Aug 07 '13 at 02:17
  • @Bergi i repeat that in other cases i used successfully the lenght property on objects – Sonia Aug 07 '13 at 02:22
  • @A.S.Roma: No you didn't, unless they were `Array`(-like) objects. – Bergi Aug 07 '13 at 02:32
  • What is this about? `Bergi` – Brian Aug 07 '13 at 02:34

2 Answers2

1

This is your result:

{"tags":{"8":"funny", "54":"lol","75":"Animals","89":"Garden"}}

That is why you will not be able to use .length.
If you had something like this LOOK AT THE DEMO:

var res = 
{ "tags": [
     {"8":"funny"}, 
     {"54":"lol"},
     {"75":"Animals"},
     {"89":"Garden"}
]}

Then .length would work on res['tags'].length.

Brian
  • 4,623
  • 8
  • 35
  • 54
  • absolutly not because such Joseph Myers said, only arrays with number as index can be count.. In this case number are string and the result will be always undefined (i've just done some proofs) – Sonia Aug 07 '13 at 02:33
  • @A.S.Roma All you did was to break the object into separate, unrelated objects with array braces around all of them. This proves nothing and is not helpful. In fact, it destroys the usefulness of the data since the tags would now be accessed by a meaningless index rather than the original code number. – Joseph Myers Aug 07 '13 at 05:03
  • @JosephMyers That is not what I did...that was not the purpose of my answer...read carefully. I mentioned that you cannot use `javascript length` unless you have something like in my example with actual object that the `length` can applied to. If you pay attention to OP's question, it is specifically mentioned that he/she knows how to count the way you provided in your answer. OP's concern was with `.length` property. – Brian Aug 07 '13 at 05:07
  • I did read carefully, and I understand what you said. Hence, I did not vote down your answer. However, it is strange your answer is accepted, because the OP says twice his only question is why it happens. I already explained why it happens. My answer contains your point about arrays as well, i.e,. that only arrays, for instance those defined using square braces, have a length property that counts everything except for their named properties. I believe that you gave the OP a mistaken impression that you were mysteriously "making it work" when it fact you completely changed the data structure. – Joseph Myers Aug 07 '13 at 05:22
  • But no worries! You're smart, and understand the issue, and I probably shouldn't have commented. I understood you, but I was just concerned that the OP might be misunderstanding you. – Joseph Myers Aug 07 '13 at 05:24
0

Anyway i know how count the number of tags

var length=0; 
for(var elemy in res['video']['tags']) length++

but the point is that i really want to know why happen this

The reason that this happens is because res['video']['tags'] is an object defined with braces {...} and so it does not possess a length property since it is not a proper array. Proper arrays (one way of defining them is using square braces []) do have a length property.

Note: Named properties (i.e., not indexed by a whole number) are not counted as part of the length of an array, but can also be added to an array. And vice versa--numbered properties can be added to an object.

Joseph Myers
  • 5,841
  • 24
  • 35
  • so the explanation could be that "tags" is such an associative array and can't be counted? – Sonia Aug 07 '13 at 02:16
  • Yes, that's correct. Associative arrays aren't normally "counted" because the identifiers aren't typically numbers. An array with elements between 0 and 499 has a length of 500; that is clear to see. But an object with elements like "document" and "alert" doesn't have a *length* just a collection of non-numbered fields. But you can still count how many there are of those non-numbered fields by doing a loop like you suggested in your own answer. – Joseph Myers Aug 07 '13 at 02:18