TypeError:无法从未定义读取属性长度

发布于 2025-01-30 14:04:33 字数 558 浏览 5 评论 0原文

我有两个字符串,这些字符串可能不确定或定义一些值。

var a ; // [Array of strings]
var b ; // [Array of strings]
var c; // result array

我需要在第三个数组中的第三个数组中的“ ABC”等静态值,如var c 定义数组A或数组B及其长度> 0。但是我的脚本正在打破,这是下面的代码行。

if((typeof a !== 'undefined' ||typeof b !== 'undefined' ) && (a.length > 0 || b.length > 0) )

当我为“ A”分配一些值时,上述LOC失败了。错误日志向我显示以下。

by: javax.script.ScriptException: TypeError: Cannot read property \"length\" from undefined\n\tat 

我该如何将这种条件更改为满足我的要求。还有更好的选择。

I am having two arrays of strings which may be undefined or defined with some value.

var a ; // [Array of strings]
var b ; // [Array of strings]
var c; // result array

I need to push some static value like "abc" in a third array like var c whenever either of the
array a or array b are defined and their length > 0. But my script is breaking , this is the line of code below.

if((typeof a !== 'undefined' ||typeof b !== 'undefined' ) && (a.length > 0 || b.length > 0) )

The above loc fails when I am assigning some value to 'a' and b is undefined. Error logs show me the below.

by: javax.script.ScriptException: TypeError: Cannot read property \"length\" from undefined\n\tat 

How can I change this condition to satisy my requirements.Is there a better away to achieve this.Also, I am using Rhino JS engine 1.13 ver.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凯凯我们等你回来 2025-02-06 14:04:33

您可以重写它,例如

if((a !== undefined && a.length > 0) || (b !== undefined && b.length > 0)) {
  c = ...
}

确保仅在每个变量不确定时才检查它们的长度。

顺便说一句,如果您使用'使用严格'模式,则只需使用a!== undefined而不是type> type a!=='undefined''

You could rewrite it like

if((a !== undefined && a.length > 0) || (b !== undefined && b.length > 0)) {
  c = ...
}

To ensure you only check the length of each variable only if they are not undefined.

By the way, if you are using 'use strict' mode, you can just use a !== undefined instead of typeof a !== 'undefined'.

猫卆 2025-02-06 14:04:33

我在您的代码中看到的问题是第一个评估,

(typeof a !== 'undefined' ||typeof b !== 'undefined' )

ab'undefined'时,它将返回true。
例如,想象一个实例,其中a未定义,但是b是一个数组。 (type a!=='undefined'||类型B!=='undefined')评估true

使用||)操作的第二个评估,从左到右开始;

(a.length > 0 || b.length > 0)

首先评估

a.length

a'undefined',因此是错误。

我会用这个

if((a !== undefined && a.length > 0) || (b !== undefined && b.length > 0)) {
  // Do something
}

The problem I see in your code is the first evaluation

(typeof a !== 'undefined' ||typeof b !== 'undefined' )

It returns true when either a or b is 'undefined'.
For instance, imagine an instance where a is undefined but b is an array; (typeof a !== 'undefined' ||typeof b !== 'undefined' ) evaluates to true.

The second evaluation which uses an OR (||) operation which starts from left to right;

(a.length > 0 || b.length > 0)

starts by evaluating

a.length

but a is 'undefined' and hence the error.

I would use this

if((a !== undefined && a.length > 0) || (b !== undefined && b.length > 0)) {
  // Do something
}
追星践月 2025-02-06 14:04:33

在JavaScript中,您还可以检查该变量的值是无效的还是未定义的。

我想这会帮助您。尝试以下操作:

if(a?.length > 0 && b?.length > 0){
    // Code here
}

In Javascript, you can also check whether the value of the variable is null or undefined with the below way.

I guess this will help you. Try this:

if(a?.length > 0 && b?.length > 0){
    // Code here
}
烈酒灼喉 2025-02-06 14:04:33

您应将组分组测试,然后组b测试。

if (
       typeof a !== "undefined" && a.length && a.length > 0
    || typeof b !== "undefined" && b.length && b.length > 0
) {
    c.push("abc");
}

并且,当犀牛支持array.isarray()时,这是一个较小且清洁的代码:

if (Array.isArray(a) && a.length > 0 || Array.isArray(b) && b.length > 0) {
    c.push("abc");
}

You should group a tests then group b tests.

if (
       typeof a !== "undefined" && a.length && a.length > 0
    || typeof b !== "undefined" && b.length && b.length > 0
) {
    c.push("abc");
}

And, as Rhino supports Array.isArray(), here is a smaller and cleaner code:

if (Array.isArray(a) && a.length > 0 || Array.isArray(b) && b.length > 0) {
    c.push("abc");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文