对象而不是数组?任何问题? [英] Object instead of array? Any problems?

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

问题描述

我一直在努力寻找一个非常复杂的页面。所有数据都是通过SQL绘制的,b / b,页面是通过VBScript构建的,然后通过javascript控制




这是一家旅游公司的页面,显示了欧洲特定地区的所有物业,周数,价格等等。数据

因地区而异;有时会有50个b $ b房产,有时只有半打房产。


交叉参考定价信息,最多客人等

财产和每个日期一直让我发疯,并且需要

感觉就像1700万个数组。


Javascript isn 这是我的专长,所以请原谅我的无知。我已经听说过
从未听说过使用过一个对象而不是一个数组,并且今天在网页上跑了过来。

。对于我正在做的事情来说,这绝对是完美的*

但我很担心 - 这似乎太容易了。我正在试图弄清楚这些陷阱是什么。


这很慢吗?它与一堆浏览器不兼容吗?大声笑 - 它只是

在经历了几天的挣扎之后似乎太容易了。如果

它不慢,它是否比阵列搜索更快?这个页面有几个数组的数据,我正在使用vbscript将它们实际构建到页面上每个日期的

onclick调用中。如果使用Object()是一种可接受的做法,它可以真正清理我的链接。


这是我正在做的事情:


var ratetable = new Object();

ratetable [" r1123"] = [800,400,300,200,0,0,0,0];

ratetable [" r6543"] = [-50,-100,-150,0,0,0,0,0];

ratetable [" r2342" ] = [50,50,50,50,25,25,25,25]

rvalue =" r6543";

乘客= 3;


alert(ratetable [rvalue] [passenger-1])


实际上,点击某个属性会调用一个javascript函数

分配rvalue,并根据

乘客数量,日期和所选房产调整旅行价格。我还有

表,显示哪些属性按日期可用,哪些日期

每个属性可用,大声笑 - 这是一团糟。这个对象很好,如果

可以使用它会很好。


我在这里缺少什么?


谢谢,


朱莉

I''ve been wrestling with a really complex page. All the data is drawn
down via SQL, the page is built via VBScript, and then controlled
through javascript.

It''s a page for a travel company that shows all the properties, weeks
available, pricing, etc. for a particular area of Europe. The data
varies widely depending on the region; at times there will be 50
properties, and at other times only a half dozen.

The cross referencing of pricing information, maximum guests, etc. per
property and per date had been driving me crazy, and was requiring
what felt like 17 million arrays.

Javascript isn''t my specialty, so please pardon my ignorance. I''d
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I''m concerned - it seems too easy. I''m trying to figure out what
the pitfalls are.

Is it slow? Is it incompatible with a bunch of browsers? lol - it just
seems too easy after struggling with this for a couple of days. And if
it isn''t slow, is it any faster than an array search? This page has
TONS of arrays, and I''m using vbscript to actually build them into the
onclick call for each date on the page. If using Object() is an
acceptable method of doing things, it could really clean up my links.

Here is the kind of thing I am doing:

var ratetable = new Object();
ratetable["r1123"] = [800,400,300,200,0,0,0,0];
ratetable["r6543"] = [-50, -100, -150, 0, 0, 0, 0, 0];
ratetable["r2342"] = [50,50,50,50,25,25,25,25]

rvalue="r6543";
passengers=3;

alert(ratetable[rvalue][passengers-1])

In reality, clicking on a property calls a javascript function which
assigns the rvalue, and adjusts the prices for the tour based on the
number of passengers, the date, and the property selected. I also have
tables that show which properties are available by date, which dates
are available per property, lol - it''s a mess. This object thingy if
it is OK to use would be SOOO great.

What am I missing here?

Thanks,

Julie

推荐答案

嗨朱莉,


6月6日下午5:45,julie.sie ... @ gmail.com < julie.sie ... @ gmail.com>

写道:
Hi Julie,

On Jun 6, 5:45 pm, "julie.sie...@gmail.com" <julie.sie...@gmail.com>
wrote:

这是一家旅游公司的页面欧洲特定地区的所有物业,周数

可用,定价等。数据

因地区而异;有时会有50个b $ b房产,有时只有半打房产。


交叉参考定价信息,最多客人等

财产和每个日期一直让我发疯,并且需要

感觉就像1700万个数组。


Javascript isn 这是我的专长,所以请原谅我的无知。我已经听说过
从未听说过使用过一个对象而不是一个数组,并且今天在网页上跑了过来。

。对于我正在做的事情来说,这绝对是完美的*

但我很担心 - 这似乎太容易了。我想弄清楚这些陷阱是什么。
It''s a page for a travel company that shows all the properties, weeks
available, pricing, etc. for a particular area of Europe. The data
varies widely depending on the region; at times there will be 50
properties, and at other times only a half dozen.

The cross referencing of pricing information, maximum guests, etc. per
property and per date had been driving me crazy, and was requiring
what felt like 17 million arrays.

Javascript isn''t my specialty, so please pardon my ignorance. I''d
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I''m concerned - it seems too easy. I''m trying to figure out what
the pitfalls are.



你应该使用一个对象!我经常使用对象然后使用
数组。原因是我可以通过键索引对象。并获得

的价值。这就像许多

语言中的哈希一样对待JavaScript对象。

You should use an object then! I use objects far more often then
arrays. The reason is I can index into the object by the "key" and get
the "value". This is treating a JavaScript object like a hash in many
languages.


它是否很慢?
Is it slow?



比通过大阵列搜索更快。

Faster than hunting through a big array.


它是否与一堆浏览器不兼容?
Is it incompatible with a bunch of browsers?



所有这些都是很久很久以前的事了。

All of them since a long, long time ago.


lol - 它只是
挣扎了几天后,
似乎太容易了。
lol - it just
seems too easy after struggling with this for a couple of days.



很高兴我很有信心! :)

A pleasure I''m sure! :)


如果

它不慢,它是否比数组搜索更快?
And if
it isn''t slow, is it any faster than an array search?



如果JavaScript引擎编写得好,那就更快。

If the JavaScript engine is written well then it is faster.


此页面有TON数组,
This page has TONS of arrays,



您在此网页中加载了多少数据?它是否明显缓慢地加载

页面?

How much data are you loading into this web page? Is it making the
page load noticeably slowly?


我正在使用vbscript将它们实际构建到

onclick调用页面上的每个日期。如果使用Object()是一种可接受的做法,它可以真正清理我的链接。


这是我正在做的事情:


var ratetable = new Object();

ratetable [" r1123"] = [800,400,300,200,0,0,0,0];

ratetable [" r6543"] = [-50,-100,-150,0,0,0,0,0];

ratetable [" r2342" ] = [50,50,50,50,25,25,25,25]

rvalue =" r6543";

乘客= 3;


alert(ratetable [rvalue] [passenger-1])$ ​​b $ b
and I''m using vbscript to actually build them into the
onclick call for each date on the page. If using Object() is an
acceptable method of doing things, it could really clean up my links.

Here is the kind of thing I am doing:

var ratetable = new Object();
ratetable["r1123"] = [800,400,300,200,0,0,0,0];
ratetable["r6543"] = [-50, -100, -150, 0, 0, 0, 0, 0];
ratetable["r2342"] = [50,50,50,50,25,25,25,25]

rvalue="r6543";
passengers=3;

alert(ratetable[rvalue][passengers-1])



有一种更简单的方法来创建具有object literal


var ratetable = {

r1123:[800,400,300,200,0,0,0,0],

r6543: [-50,-100,-150,0,0,0,0,0],

r2342:[50,50,50,50,25,25,25,25]

};

There is an easier ways to create objects with an object literal

var ratetable = {
r1123: [800,400,300,200,0,0,0,0],
r6543: [-50, -100, -150, 0, 0, 0, 0, 0],
r2342: [50,50,50,50,25,25,25,25]
};


实际上,点击某个属性会调用一个javascript函数

指定rvalue,并调整th基于

乘客数量,日期和所选房产的旅行价格。我还有

表,显示哪些属性按日期可用,哪些日期

每个属性可用,大声笑 - 这是一团糟。如果

可以使用这个对象,那就太好了。
In reality, clicking on a property calls a javascript function which
assigns the rvalue, and adjusts the prices for the tour based on the
number of passengers, the date, and the property selected. I also have
tables that show which properties are available by date, which dates
are available per property, lol - it''s a mess. This object thingy if
it is OK to use would be SOOO great.



对象很好。它是

语言的最佳功能之一。


Peter

The object thingy is ok. It is one of the best features of the
language.

Peter


6月7日下午1:43,Peter Michaux< petermich ... @ gmail.comwrote:
On Jun 7, 1:43 pm, Peter Michaux <petermich...@gmail.comwrote:

嗨Julie,


6月6日下午5:45,julie.sie ... @ gmail.com < julie.sie ... @ gmail.com>

写道:
Hi Julie,

On Jun 6, 5:45 pm, "julie.sie...@gmail.com" <julie.sie...@gmail.com>
wrote:



[...]

[...]


Javascript不是我的专长,所以请原谅我的无知。我已经听说过
从未听说过使用过一个对象而不是一个数组,并且今天在网页上跑了过来。

。对于我正在做的事情来说,这绝对是完美的*

但我很担心 - 这似乎太容易了。我想弄清楚这些陷阱是什么。
Javascript isn''t my specialty, so please pardon my ignorance. I''d
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I''m concerned - it seems too easy. I''m trying to figure out what
the pitfalls are.



你应该使用一个对象!


You should use an object then!



[...]

[...]


是否与a不兼容一堆浏览器?
Is it incompatible with a bunch of browsers?



所有这些都是很久很久以前的事了。


All of them since a long, long time ago.



我认为你的意思是*无*。 :-)


几乎javascript中的所有内容都是一个对象,所以如果一个特定的

浏览器不支持对象......

I think you mean *none* of them. :-)

Just about everything in javascript is an object, so if a particular
browser doesn''t support objects...


lol - 它只是

在经历了几天的挣扎之后似乎太容易了。
lol - it just
seems too easy after struggling with this for a couple of days.



很高兴我很有信心! :)


A pleasure I''m sure! :)


如果

它不慢,它是否比数组搜索更快?
And if
it isn''t slow, is it any faster than an array search?



如果JavaScript引擎写得好,那就更快了。


If the JavaScript engine is written well then it is faster.



您可以使用for循环搜索数组:


var elementValue;

for(var i = 0,len = someArray.length; i< len; i ++){

elementValue = someArray [i];

/ *使用elementValue执行某些操作* /

}


对等的对象是for..in循环:


var propertyValue;

for(someObject中的var属性){

propertyValue = someObject [property];

/ *使用objectProperty或propertyValue执行某些操作* /

}


但请注意,如果someObject的原型(或其原型链中的任何原型)已添加了属性(糟糕的做法,但你可能会绊倒它的b $ b),你也会迭代这些属性。

-

Rob

You can search an array using a for loop like:

var elementValue;
for (var i=0, len=someArray.length; i<len; i++){
elementValue = someArray[i];
/* do something with elementValue */
}

The object equivalent is a for..in loop:

var propertyValue;
for (var property in someObject) {
propertyValue = someObject[property];
/* do something with objectProperty or propertyValue */
}

But be aware that if someObject''s prototype (or any prototype in its
prototype chain) has had properties added (bad practice but you may
stumble across it), you will iterate over those properties too.
--
Rob


6月6日晚上9点33分,RobG< r ... @ iinet.net.auwrote:
On Jun 6, 9:33 pm, RobG <r...@iinet.net.auwrote:

6月7日下午1:43,Peter Michaux& LT; petermich ... @ gmail.comwrote:
On Jun 7, 1:43 pm, Peter Michaux <petermich...@gmail.comwrote:

嗨朱莉,
Hi Julie,


6月6日下午5:45,julie.sie ... @ gmail.com < julie.sie ... @ gmail.com>

写道:
On Jun 6, 5:45 pm, "julie.sie...@gmail.com" <julie.sie...@gmail.com>
wrote:



[...]

[...]


Javascript不是我的专长,所以请原谅我的无知。我已经听说过
从未听说过使用过一个对象而不是一个数组,并且今天在网页上跑了过来。

。对于我正在做的事情来说,这绝对是完美的*

但我很担心 - 这似乎太容易了。我想弄清楚这些陷阱是什么。
Javascript isn''t my specialty, so please pardon my ignorance. I''d
never heard of using an object instead of an array, and ran across it
in a web page today. It is absolutely *perfect* for what I am doing,
but I''m concerned - it seems too easy. I''m trying to figure out what
the pitfalls are.


你应该使用一个对象!
You should use an object then!



[...]


[...]


是否与a不兼容一堆浏览器?
Is it incompatible with a bunch of browsers?


所有这些都是很久很久以前的事了。
All of them since a long, long time ago.



我认为你的意思是*无*。 :-)


I think you mean *none* of them. :-)



嗯。是的,是的,我做到了。谢谢,Rob。


彼得

Umm. Yes, yes I did. Thanks, Rob.

Peter


这篇关于对象而不是数组?任何问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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