gpt4 book ai didi

javascript - push 或 concrat 数组的 undefined variable

转载 作者:行者123 更新时间:2023-11-29 18:45:48 25 4
gpt4 key购买 nike

无法弄清楚如何将 PROTOCOL 的所有变量放入一个新数组中,该数组具有 N 个 PROTOCOL 的循环函数(与序列 1 相同的形式)。问题是,我在所有情况下都会遇到不同的失败(请参阅代码中的取消注释输出),并且在我查看循环后的 console.log 后,“动态”数组的所有输入都消失了。作为初学者感到沮丧。感谢帮助

let NR_i = 0;
let NR_i_end = 7;
let PROTOCOL = new Array();
let PROTOCOL_HTML = new Array();
let PROTOCOL_CSS = new Array();
let PROTOCOL_JS = new Array();
let PROTOCOL_PHP = new Array();
let PROTOCOL_MySQL = new Array();
let PROTOCOL_CindyJS = new Array();
let PROTOCOL_C_Cpp = new Array();
let allgemein = [];

//-- Sequence 1 --//
PROTOCOL = [{name:"HTML", index:[].sort()},
{name:"CSS", index:[].sort()},
{name:"JS", index:["document", "getElementById", "var",
"message", "console", "log"].sort()},
{name:"PHP", index:["echo"].sort()},
{name:"MySQL", index:[].sort()},
{name:"CindyJS", index:[].sort()},
{name:"Python", index:[].sort()},
{name:"C_Cpp", index:[].sort()}
];
while(NR_i <= NR_i_end) {
var nameX_2 = [];
if (PROTOCOL[NR_i].index.length != 0)
nameX_2 = 'PROTOCOL_' + PROTOCOL[NR_i].name;
// OUTPUT - OK: PROTOCOL_JS and so on.

//nameX_2.push(PROTOCOL[NR_i].index);
// OUTPUT: TypeError: nameX_2.push is not a function

//window[nameX_2].push(PROTOCOL[NR_i].index);
// OUTPUT: TypeError: Cannot read property 'push' of undefined

//nameX_2 = nameX_2.concat(PROTOCOL[NR_i].index);
// OUTPUT: PROTOCOL_JSconcat,console, ...
// Why is PROTOCOL_JS also in? that disturb the hole array

//nameX_2 = nameX_2.concat(window[PROTOCOL[NR_i].index]);
// OUTPUT: PROTOCOL_JSundefined

//nameX_2 = [...nameX_2, ...window[PROTOCOL[NR_i].index]];
// OUTPUT: TypeError: window[PROTOCOL[NR_i].index] is not iterable

nameX_2 = [...nameX_2, ...PROTOCOL[NR_i].index];
// OUTPUT: ["P", "R", "O", "T", "O", "C", "O", "L",
// "_", "J", "S", "concat", "console", and so on
// Again. Why is PROTOCOL_JS also in? that disturb the hole array

console.log("protocolname_3: ", nameX_2);
}
NR_i++;
}
console.log("PROTOCOL_HTML: ",PROTOCOL_HTML);
console.log("PROTOCOL_CS: ",PROTOCOL_CSS);
console.log("PROTOCOL_JS: ",PROTOCOL_JS);
console.log("PROTOCOL_PHP: ",PROTOCOL_PHP);
console.log("PROTOCOL_MySQL: ",PROTOCOL_MySQL);
console.log("PROTOCOL_CindyJS: ",PROTOCOL_CindyJS);
console.log("PROTOCOL_C_Cpp: ",PROTOCOL_C_Cpp);
console.log("nameX_2: ",nameX_2);
// OUTPUT: all empty =/
//-- Sequence 2 --//
// Same struct as Sequence 1, just with different name inputs
//-- Sequence N --//
// Same struct as Sequence 1, just with different name inputs

最佳答案

我不是 100% 确定你在问什么,但这可能会有所帮助

PROTOCOL = [{name:"HTML", index:[].sort()},
{name:"CSS", index:[].sort()},
{name:"JS", index:["document", "getElementById", "var",
"message", "console", "log"].sort()},
{name:"PHP", index:["echo"].sort()},
{name:"MySQL", index:[].sort()},
{name:"CindyJS", index:[].sort()},
{name:"Python", index:[].sort()},
{name:"C_Cpp", index:[].sort()}
];

arrays = new Array();
for (i = 0; i < n; i++) {
arrays[i] = new Array();
PROTOCOL.forEach(function(value){
arrays[i].push(value);
});
};

关于javascript - push 或 concrat 数组的 undefined variable ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54154179/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com