类型错误: 无效的“in”操作数 obj
TypeError: invalid 'in' operand obj
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') { ... }
相关文章:
- Node.js's Buffer.writeFloatBE in Javascript
- Setting default onclick behavior for <img> tag in gene
- Lucene Search in Alfresco
- grep in JQuery to C#
- write HTML in JavaScript
- jQuery setTimeOut: in for-loop
- SetTimeout and clearTimeout in Javascript
- lightbox in html 5 and javascript
- HTMLInputElement in IE7
- Mongodb$in以与数组中相同的顺序获取结果
- element.dataset in Internet Explorer
- jQuery parent() in table
- Instagram oauth flow in angularjs
- In循环的In运算符前后
- 如何使用for in循环在javascript中显示对象中的对象
- Jquery change onchange in onload
- TypeError: 使用 Jquery JSON 获取数据时无效的 'in' 操作数 obj
- 类型错误: 无效的“in”操作数 obj
- 无效的“in”操作数 obj JQUERY - 尝试从 JSON 获取数据时出现类型错误
- 获取"TypeError: invalid 'in'操作数a"在WordPress