JScript阵列问题

时间:2012-04-22 14:00:41

标签: javascript arrays com jscript

重新发布的工作代码

(我正试图从我的ActiveX组件中获取数组,但没有运气。当我运行脚本时,我得到空的Message Box,但我知道数组在那里:)

var objMain, objAdapt, lgCount, stMsg = "";

objMain = new ActiveXObject("nnetcom.oMain");

objMain.UnlockComponent("xxx-xxxxx-xxxxx-xx");

objAdapt = new ActiveXObject("nnetcom.oNetworkAdapter");

objAdapt.GetNetworkAdapters(); // Collects Network Adapters

vrAdapters = objAdapt.cName; // cName holds collected Network Adapter names

var vrAdaptVB = new VBArray(vrAdapters);

var vrAdaptJS = vrAdaptVB.toArray();

for (lgCount in vrAdaptJS) {
  stMsg = stMsg + vrAdaptJS[lgCount] + '\r\n'; 
}

WScript.Echo(stMsg);

objAdapt = null
objMain = null

3 个答案:

答案 0 :(得分:2)

var vrAdaptVB = new VBArray(vrAdapters);

var vrAdaptJS = vrAdaptVB.toArray();

stMsg = "";

for (lgCount = 0; lgCount < vrAdaptJS.length; ++lgCount) {
 stMsg = stMsg + vrAdaptJS[lgCount] + '\r\n';
}

答案 1 :(得分:0)

Per Hans评论,

for (lgCount in vrAdapt)
{
    stMsg = stMsg + vrAdapt[lgCount] + '\r\n';
}

应该是:

for (lgCount in vrAdapt)
{
    stMsg = stMsg + lgCount + '\r\n';
}

因为lgCount是元素,而不是索引。

我的错误。这个例子有效:

var list = {a:1,b:2,c:3,d:4,e:5};
var msg = "";
for (i in list) {
    msg = msg + list[i]; 
}
//msg = 12345

msg = "";
for (i in list) {
    msg = msg + i; 
}
//msg = abcde

var list = [1,2,3,4,5];
msg = "";
for (i in list) {
    msg = msg + i; 
}
//msg = 01234

msg = "";
for (i in list) {
    msg = msg + list[i]; 
}
//msg = 12345

答案 2 :(得分:0)

从我的第一个发布的答案中最简单的方法是:

var vrAdaptVB = new VBArray(vrAdapters);

var vrAdaptJS = vrAdaptVB.toArray();

stMsg = "";

for (lgCount in vrAdaptJS) {
 stMsg = stMsg + vrAdaptJS[lgCount] + '\r\n'; 
}

此示例也适用于我!

List Network Adapters working example