admin管理员组

文章数量:1318984

       json= new Gson().toJson(name);

where name is of type string.

I am getting error saying "TypeError: invalid 'in' operand obj "

error is in the given part of java script

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

I have also tried

   response=JSON.parse(response); 

which is not working.

       json= new Gson().toJson(name);

where name is of type string.

I am getting error saying "TypeError: invalid 'in' operand obj "

error is in the given part of java script

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

I have also tried

   response=JSON.parse(response); 

which is not working.

Share Improve this question edited Jun 3, 2015 at 15:07 IARI 1,3771 gold badge19 silver badges39 bronze badges asked Nov 26, 2013 at 7:03 user2814799user2814799 5592 gold badges7 silver badges17 bronze badges 4
  • what is responseJson – Arun P Johny Commented Nov 26, 2013 at 7:04
  • response object which i am getting from servlet – user2814799 Commented Nov 26, 2013 at 7:07
  • element of type object – user2814799 Commented Nov 26, 2013 at 7:09
  • As the error suggests, obj doesn't seem to be a valid operand for in. – Felix Kling Commented Nov 26, 2013 at 7:18
Add a ment  | 

4 Answers 4

Reset to default 2

by link In operator you can see that

The in operator returns true if the specified property is in the specified object.

in your code ( length - 1 ) in obj you try checking that property (length - 1) that numeric in your obj

I think obj is a string, so it has the length property, but does not have numeric properties, so you must catch this case

I closed the code "( length - 1 ) in obj" between parentheses.

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

That's working fine for me.

In my case the object was not empty but I needed to add double quotes on the object's key , example :

obj = {params : "paramsInputPreData"}

The Above will produce an error but the one below was okay :

obj = {"params" : "paramsInputPreData"}

so for me the key needed to be quoted as "params"

Hope it helps someone

When you have this error, you need to check the type of your variables, etc., you can always do something like this:

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

本文标签: javascriptTypeError invalid 39in39 operand objStack Overflow