类型错误: 无效的“in”操作数 obj

TypeError: invalid 'in' operand obj

本文关键字:in 操作数 obj 错误 无效 类型      更新时间:2023-10-06
       json= new Gson().toJson(name);

其中名称为字符串类型。

我收到错误说"类型错误:无效的'in'操作数 obj">

错误在 Java 脚本的给定部分

 return type === "array" || type !== "function" &&
     ( length === 0 ||
     typeof length === "number" && length > 0 && ( length - 1 ) in obj );

我也试过

   response=JSON.parse(response); 

这是行不通的。

通过链接 在运算符中,您可以看到

如果指定的属性位于指定的对象中,则 in 运算符返回 true。

在您的代码( length - 1 ) in obj中,您尝试在 obj 中检查该属性(length - 1)该数字

我认为obj是一个string,所以它具有length属性,但没有数字属性,因此您必须抓住这种情况

我在括号之间关闭了代码"(长度 - 1 ( in obj"。

return type === "array" || type !== "function" &&
     ( length === 0 ||
     typeof length === "number" && length > 0 && (( length - 1 ) in obj) );

这对我来说很好用。

在我的情况下,对象不为空,但我需要在对象的键上添加双引号,例如:

obj = {params : "paramsInputPreData"}

上面会产生一个错误,但下面的一个还可以:

obj = {"params" : "paramsInputPreData"}

所以对我来说,键需要被引用为"参数">

希望它对某人有所帮助

当你遇到这个错误时,你需要检查你的变量的类型等,你总是可以这样做:

if (typeof x == 'string' || typeof x == 'number') { ... }