计数的Javascript对象数组 [英] Javascript Counting Array of Objects

查看:120
本文介绍了计数的Javascript对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直有麻烦计数此数组中的服务器端JavaScript中的对象的数量。

下面是一个被解析出使用我想统计数组的JSON对象。

注:该对象是对象的形式,而不是JSON字符串形式。

JSON对象:

<$p$p><$c$c>[{\"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}]

我通常使用myObject.length来算这个类型的数组,但是这是行不通的。

 的Response.Write(optionsQuotes.length);

以上code正在返回的21339646结果作为计数,当数组的实际计数为5。

我宁可不要遍历数组来算吧,因为我通过它后来为了绘制表格循环,我需要知道的最后一次迭代表抽签开始前。

任何想法?

编辑:

  //这里是我gettnig对象的数组...
变种MyObj中= common.getMyObj(参数1,参数2);

我序列化的对象为表示数组的内容的目的。

myObj.constructor为Array。

这是在服务器端也BTW。


解决方案

ECMAScript中不处理PHP之类的assocative数组长度那样 - 无论是使用具有财产。长度真正的名单,设置属性。长度手动在JSON作为您填充性能的对象,或者做一个for..in循环,并确保使用.hasOwnProperty并增加一些反。

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.

解决方案

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.

这篇关于计数的Javascript对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆