- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将一些旧的 VBScript 重写为 ASP.NET 应用程序的 Javascript,其中有一行我不知道如何翻译,而且我什至不完全肯定它在做什么。
该应用程序本质上允许用户输入新的员工编号以添加到数据库中,然后为其分配用户权限。不要问我为什么代码这么乱,不是我最初写的,我只是想让它在 Chrome 中工作
这是迄今为止我已成功翻译的相关代码:
if(form1.txtEmpno.value != ""){
var oOption;
oOption = document.createElement("OPTION");
oOption.text=form1.txtEmpno.value;
oOption.value=form1.txtEmpno.value;
form1.lstActive.add (oOption);
oOption = document.createElement("OPTION");
oOption.text="";
oOption.value="";
form1.lstPerms.add (oOption);
redim preserve arrUsers(1,ubound(arrUsers,2)+1);
arrUsers(0,ubound(arrUsers,2)) = form1.txtEmpno.value;
arrUsers(1,ubound(arrUsers,2)) = "";
form1.txtEmpno.value = "";
oOption = null;
}
这是有问题的行:
redim preserve arrUsers(1,ubound(arrUsers,2)+1);
最佳答案
MSDN 将 ReDim [Preserve] varname(subscripts)
定义为:
The
ReDim
statement is used to size or resize a dynamic array that has already been formally declared using aPrivate
,Public
, orDim
statement with empty parentheses (without dimension subscripts). You can use theReDim
statement repeatedly to change the number of elements and dimensions in an array.If you use the
Preserve
keyword, you can resize only the last array dimension, and you can't change the number of dimensions at all. For example, if your array has only one dimension, you can resize that dimension because it is the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension and still preserve the contents of the array.
JavaScript 中的数组与 VBScript 的数组具有不同的语义,特别是它们实际上比真正的数组更接近向量
,而且 JavaScript 不提供真正的 N 维数组:相反,您可以使用使用交错数组(数组内数组)。这意味着您的 VBScript 无法在语法上转换为 JavaScript。
这是 VBScript 中的相关代码:
ReDim Preserve arrUsers(1,ubound(arrUsers,2)+1)
arrUsers(0,ubound(arrUsers,2)) = form1.txtEmpno.value
arrUsers(1,ubound(arrUsers,2)) = ""
我们看到arrUsers
是一个二维数组。这需要转换为交错数组,但您还没有发布定义和初始化 arrUsers 的代码,也没有发布稍后如何使用它,所以我只能进行假设。
它看起来向最后一个维度添加了 1 个元素,但代码似乎只使用了 [1]
下标中的额外空间(即,它只需要某些值的额外维度空间的第 0 维而不是所有值),这使得这更简单,因为您不需要迭代每个第 0 维下标。
JavaScript 数组有许多我们将使用的函数属性,特别是 push
:它将一个元素附加到数组的末尾(如果需要,在内部增加缓冲区),以及 pop
从数组中删除最后一个(最高索引)元素(如果数组为空,则为 NOOP):
var arrUsers = [ [], [] ]; // empty, staggered 2-dimensional array
...
arrUsers[0].push( form1.txtEmpno.value );
arrUsers[1].pop();
简单得多。
但是,如果此数组只是存储和表示数据的某些内部模型的一部分,那么您应该利用 JavaScript 对象原型(prototype)而不是使用数组索引,因为这使得代码具有自描述性,例如:
var User = function(empNo, name) {
this.employeeNumber = empNo;
this.name = name;
};
var users = [];
users.push( new User(1, "user 1") );
users.push( new User(23, "user 23") );
...
for(var i = 0; i < users.length; i++ ) {
alert( users[i].name );
}
关于相当于 VBS redim 保留的 Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33590897/
我正在使用函数来创建数组。输入也是一个数组。运行时它给我一个 invalid ReDim 编译错误。在此之前,它在子例程中运行,ReDim 运行良好,但现在我在函数中更改了它,它给出了 invalid
这似乎是关于这个主题的第 100 万个问题,但搜索并没有帮助我。 我正在尝试调整二维数组的最后一个维度的大小,但在 ReDim Preserve 行中总是出现“索引超出范围”错误。 Dim arrCu
假设我有这样一段代码: Dim resultArray() As Double ReDim resultArray(1 to 10) As Double Dim i As Integer For i
我正在尝试重新保存二维数组。在我的代码的最顶部我有: Dim BayQuestionArray() As Variant Dim numberofbay As Double 我有两个按钮,第一个按钮用
我在 Win7 下使用 Access 2010。我发现我可以在运行时通过简单地调用 ReDim arrayName(x) 来确定我的数组的维度。无需先将数组声明为 Dim arrayName() .
我不知道如何填充多维数组。这是我刚开始理解的代码,但我不知道出了什么问题,除了一个循环之外它不起作用。 Sub test() Dim Arr1 As Variant Dim Row A
当我第一次创建数组时,我知道最好将数据类型设置为默认 Variant类型很慢,否则最好在 VBA 中避免 我注意到 Redim t(1 To 10) As String 似乎具有相同的效果 Dim t
您能否向我解释为什么这个简单的 VBA 代码在最后一行失败(“下标超出范围”): Dim test() As Variant ReDim test(0, 1) test(0,0
我正在尝试在QTP/VB脚本中重新创建数组,但我不断收到错误消息:Expected Statement。 在过去的几个小时中,我已经遍历了一百次代码,而我只是没有看到丢失的内容,而且这里的办公室似乎没
我在 Excel 宏 (VBA) 中有一个情况,我想对一个数组进行维度标注,其中维度数和每个维度的边界在运行时确定。我让用户通过为每个选项类型创建一列并填写下面的可能性来指定一系列组合选项。列数和选项
我正在看别人的vba excel代码。他们在两个循环中都执行 ReDim Preserve dataMatrix(7, i) 。这是做什么的? 另外,第二个循环似乎只是覆盖了第一个循环中的数据,对吗?
我在 VBA (Excel 365) 中有一个二维数组。 它给了我一条错误消息Subscript out of Range 代码停在 ReDim 行。 ReDim arrs(1 To 1, 1 to
我需要从处理 .txt 期间生成的变量值填充数组文件。然而,上限取决于输入线。我怎样才能使以下事情起作用? Dim myArr As Variant ReDim Preserve myArr(1)
我正在尝试将名称及其值插入二维数组: ____________ | name| value| index 0 |name1|value1| index 1 |name2|value2| index
问题:我有以下两个函数,transposeArray3D 和 ReDimPreserve3D 应该可以有效地 ReDim 在所有维度上保留 3D 数组。但是,即使我尝试在转置函数中重新设置值,这也会删
我正在使用这个有效的代码,但我在数组末尾得到了很多空行:ary有没有办法调整大小ary循环内的数组以避免条件不满足的额外空行? Dim myTable As ListObject Dim myArra
我正在尝试将一些旧的 VBScript 重写为 ASP.NET 应用程序的 Javascript,其中有一行我不知道如何翻译,而且我什至不完全肯定它在做什么。 该应用程序本质上允许用户输入新的员工编号
如果您想重置 bool 值数组,哪个更快,重新调整数组或枚举并重置值? 我已经运行了一些测试,他们似乎表明 redim 速度要快得多,但我不相信这不是我运行测试的结果。 我的测试似乎表明 redim
我正在尝试将数据从 2 个 double 组移动到 2 个不同的 double 组。我不确定大小是多少,因为我正在从第一个数组中随机抽取样本并将其放入第二个数组中。 当我添加 ReDim Preser
通过在全新工作簿的 IDE 中输入以下内容,我能够以某种方式使 Excel 2010 崩溃: private sub foo redim v(,1 to 3) 好吧,你不太可能输入这个...但
我是一名优秀的程序员,十分优秀!