views:

407

answers:

4

I am been having trouble counting the number of objects in this array in server-side javascript.

Below is a JSON object which was parsed out using the array that I am trying to count.

NOTE: The object is in object form, not JSON string form.

JSON Object:

[{"dataSymbol":"21135103","isHoliday":false,"isIPO":false,"lastTradeTime":40073.49652777778,"strikePrice":"33.00","last":"1.30","change":"0.20","changePct":"18.1818","lastRaw":1.3,"ask":"1.40","bid":"1.30","lastTime":40073.49652777778,"tick":0,"openInterest":"13.6K","volume":"80311","expDate":40194,"coName":"AJR Jan0 33.0 C"},
 {"dataSymbol":"21339645","isHoliday":false,"isIPO":false,"lastTradeTime":40073.50479166866,"strikePrice":"6.00","last":"2.11","change":"0.01","changePct":"0.4762","lastRaw":2.11,"ask":"2.15","bid":"2.10","lastTime":40073.50479166866,"tick":0,"openInterest":"105.00","volume":"62313","expDate":40285,"coName":"EK Apr0 6.0 C"},
 {"dataSymbol":"13511861","isHoliday":false,"isIPO":false,"lastTradeTime":40073.489583333336,"strikePrice":"113.00","last":"1.41","change":"-6.34","changePct":"-81.8065","lastRaw":1.41,"ask":"7.60","bid":"7.45","lastTime":40073.489583333336,"tick":0,"openInterest":"805.00","volume":"62975","expDate":40138,"coName":"SPY Nov8 113.0 P"},
 {"dataSymbol":"20718334","isHoliday":false,"isIPO":false,"lastTradeTime":40073.49375,"strikePrice":"40.00","last":"1.42","change":"-0.05","changePct":"-3.4014","lastRaw":1.42,"ask":"1.46","bid":"1.44","lastTime":40073.49375,"tick":0,"openInterest":"116.1K","volume":"60470","expDate":40194,"coName":"QQQQ Jan0 40.0 P"},
 {"dataSymbol":"20348966","isHoliday":false,"isIPO":false,"lastTradeTime":40073.47708333333,"strikePrice":"41.00","last":"2.39","change":"-0.06","changePct":"-2.449","lastRaw":2.39,"ask":"2.45","bid":"2.42","lastTime":40073.47708333333,"tick":-1,"openInterest":"4.6K","volume":"60320","expDate":40257,"coName":"QQQQ Mar0 41.0 P"}]

I usually use myObject.length to count this type of array, but that is not working.

Response.Write(optionsQuotes.length);

The above code is returning a result of 21339646 as the count, when the actual count of the array is 5.

I would rather not have to loop through the array to count it, because I am looping through it later in order to draw a table, and I need to know the last iteration before the table draw begins.

Any ideas?

EDIT:

//here is where I am gettnig the array of objects...
var myObj =  common.getMyObj("param1", "param2");

I serialized the object for the purpose of showing the contents of the array.

myObj.constructor is an Array.

This is on the server side also BTW.

+2  A: 

ECMAScript doesn't handle the length of "assocative" arrays like PHP does - either use a real list that has a .length property, set the .length property manually in the JSON as you populate properties in the object, or do a for..in loop and make sure to use .hasOwnProperty and increment some counter.

meder
not relevant as Shane wants to determine the length of the enclosing array
Christoph
I looped through the array and iterated my counter, which worked.
Shane Larson
What are you using to evaluate it?
meder
On the server side, this is just an array of objects. I am not evaluating it, since it's an actual array, not a JSON string. I ended up using your example, and i asked my boss and he indicated that you are correct. You cannot use .length on an associative array in JScript. Thanks
Shane Larson
@Shane: The array in your question isn't an associative array. There is no such thing as associative arrays in javascript. There are arrays and there are objects.
Chetan Sastry
+2  A: 

Mhh... maybe is not a JSON object but an string and the length that is returning is the length of the string and not of the json array

With prototype you need to do something like

var data = '{ "name": "Violet", "occupation": "character" }'.evalJSON();
data.length

but this obviously is depending of the framework that you are using.

4NDR01D3
the JSON string was just to demonstrate the contents of the object that I am attempting to count. It is an actual object within the code
Shane Larson
A: 

The bug must be somewhere else as the following

<script>
var foo = eval('[{"dataSymbol":"21135103","isHoliday":false,"isIPO":false,"lastTradeTime":40073.49652777778,"strikePrice":"33.00","last":"1.30","change":"0.20","changePct":"18.1818","lastRaw":1.3,"ask":"1.40","bid":"1.30","lastTime":40073.49652777778,"tick":0,"openInterest":"13.6K","volume":"80311","expDate":40194,"coName":"AJR Jan0 33.0 C"},{"dataSymbol":"21339645","isHoliday":false,"isIPO":false,"lastTradeTime":40073.50479166866,"strikePrice":"6.00","last":"2.11","change":"0.01","changePct":"0.4762","lastRaw":2.11,"ask":"2.15","bid":"2.10","lastTime":40073.50479166866,"tick":0,"openInterest":"105.00","volume":"62313","expDate":40285,"coName":"EK Apr0 6.0 C"},{"dataSymbol":"13511861","isHoliday":false,"isIPO":false,"lastTradeTime":40073.489583333336,"strikePrice":"113.00","last":"1.41","change":"-6.34","changePct":"-81.8065","lastRaw":1.41,"ask":"7.60","bid":"7.45","lastTime":40073.489583333336,"tick":0,"openInterest":"805.00","volume":"62975","expDate":40138,"coName":"SPY Nov8 113.0 P"},{"dataSymbol":"20718334","isHoliday":false,"isIPO":false,"lastTradeTime":40073.49375,"strikePrice":"40.00","last":"1.42","change":"-0.05","changePct":"-3.4014","lastRaw":1.42,"ask":"1.46","bid":"1.44","lastTime":40073.49375,"tick":0,"openInterest":"116.1K","volume":"60470","expDate":40194,"coName":"QQQQ Jan0 40.0 P"},{"dataSymbol":"20348966","isHoliday":false,"isIPO":false,"lastTradeTime":40073.47708333333,"strikePrice":"41.00","last":"2.39","change":"-0.06","changePct":"-2.449","lastRaw":2.39,"ask":"2.45","bid":"2.42","lastTime":40073.47708333333,"tick":-1,"openInterest":"4.6K","volume":"60320","expDate":40257,"coName":"QQQQ Mar0 41.0 P"}]');
document.writeln(foo.length);
</script>

yields the correct value.

Christoph
A: 

I think you need to eval the string. Could 21339645 be the number of characters?

Josh Stodola