javascript - 空数组的Javascript测试不起作用
问题描述
我正在使用具有 Javascript ES5 的工具。我正在尝试通过 if 条件测试数组是否为空。if 之前的数组输出状态为“null”(见下面的测试结果);但是,当对“null”进行 if 条件检查时,它认为应该在不应该继续处理时继续处理。我已经通过 if 语句以各种方式检查了一个空数组,当我转储调试数据并在数组上指定 [_OFNBRindex](当前已注释掉)时,它仍然通过了测试并由于空数组值而中止。”
前两个语句用于测试代码。1)。一个空字符串,(给出问题的字符串),2),一个编号列表。编号列表将起作用。此外,指定“[_OFNBRindex]”的注释掉的语句也会出错。
对于将停止空数组以便它们使用“else”语句的 IF 条件,我缺少什么?
var _OFNBR = ""; // test value #1
// var _OFNBR = "100\n2\n\n40"; // test value #2
var _OFNBRindex = 0;
_OFNBR = _OFNBR.replace(/^[ \r\n\v\t\f\uFEFF\xA0]+|[ \r\n\v\t\f\uFEFF\xA0]+$/gi, "");
var _OFNBRLst = _OFNBR.match(/[0-9]+/g);
var _debug_text_msg = "Info: The DATA DUMP ref code: " + "idx " + "\"" + _OFNBRindex + "\"" + "\"" + _OFNBRLst + "\"";
java.lang.System.out.println(_debug_text_msg);
if ((_OFNBRLst !== undefined) || (_OFNBRLst !== "null") || (_OFNBRLst !== "") || (_OFNBRLst !== null)) {
var _debug_text_msg = "Info: The NOT NULL ref code: " + "idx " + "\"" + _OFNBRindex + "\"" + "\"" + _OFNBRLst + "\"";
//var _debug_text_msg = "Info: The NOT NULL ref code: " + "idx " + "\"" + _OFNBRindex + "\"" + "\"" + _OFNBRLst[_OFNbrindex] + "\"";
java.lang.System.out.println(_debug_text_msg);
} else { // OFNBRLst is null, set to null
var _debug_text_msg = "Info: The IS NULL ref code: " + "idx " + "\"" + _OFNBRindex + "\"" + "\"" + _OFNBRLst + "\"";
java.lang.System.out.println(_debug_text_msg);
} // end if for OFNBRLst being null
_OFNBRindex++; // increment the _OFNBR index list.
// results
// First statement test #1
// Info: The DATA DUMP ref code: idx "0""null"
// Info: The NOT NULL ref code: idx "0""null"
// Second statement test #1
// Info: The DATA DUMP ref code: idx "0""100,2,40"
// Info: The NOT NULL ref code: idx "0""100,2,40"
解决方案
看起来您只有一些需要 AND 的 OR (如果您需要其他!==
检查)。但是,您使用的String.protoype.match的返回值是数组(如果找到匹配项)或null
. 所以你不需要任何!==
字符串检查。
所以改变这一行:
if ((_OFNBRLst !== undefined) || (_OFNBRLst !== "null") || (_OFNBRLst !== "") || (_OFNBRLst !== null)) {
对此:
if (_OFNBRLst !== null) {
推荐阅读
- c# - ContainerVisual.Offset 和 Visual.VisualOffset 有什么区别?
- docker - 如何构建 docker 映像并推送到 azure 容器注册表
- sql - 我的半高级 PostgreSQL 查询有什么问题?(与计算时间和时间间隔有关。)
- rust - 流上的 tokio::timeout 总是发生
- paypal - 如何收集信用卡信息并向拍卖的获胜者收费
- reactjs - 使用复合主键将数据写入 DynamoDB
- c - 为什么 waitpid 挂起而不获取所有子进程?
- sql - Presto SQL JOIN 在具有 NULL 值的列上
- node.js - POST 请求不适用于 POSTMAN 上的 JSON
- github - Travis-CI 不允许我重建测试