Invalid operand to ‘in’: Object expected using json values

问题内容:

I am using JSON values like below

$.each(UMSGroupDetails, function (index, element) {
            if (element.GroupName == UserGrouop.CT) {
                umsGroupNameTo = element.GroupName;
                umsGroupIdTo = element.GroupID;
            }
        });

and its json data like this

[{"GroupName":"CMM","GroupID":625},{"GroupName":"Construction_Eng","GroupID":626},{"GroupName":"DY. CTO","GroupID":624},{"GroupName":"Fiber Engineer","GroupID":622},{"GroupName":"Fiber Lead","GroupID":623}]

but getting error as

Invalid operand to ‘in’: Object expected

Please suggest what is wrong here

MORE INFO

 var LoginUserName = "Nadeem5.khan";
    var UMSGroupDetailsObj = function () { return [{"GroupName":"CMM","GroupID":625},{"GroupName":"Construction_Eng","GroupID":626},{"GroupName":"DY. CTO","GroupID":624},{"GroupName":"Fiber Engineer","GroupID":622},{"GroupName":"Fiber Lead","GroupID":623}]; }
    var UMSGroupDetails = JSON.stringify(UMSGroupDetailsObj());

    var CurrentGroupName = "Fiber Engineer";
    var CurrentGroupID = "622";
    var UMSLocationDetailsObj = function () { return [{"LocationName":"Kalyan","LocationID":"INMUKLYN01"}]; }
    var UMSLocationDetails = JSON.stringify(UMSLocationDetailsObj())

问题评论:

    
UserGrouop.CT is this spelled correctly?
    
Also, this has nothing to do with json. That’s an array containing objects.
    
Are these variables defined previously umsGroupNameTo, umsGroupIdTo with var. Other than that and UserGrouop.CT as mentioned above you’re fine
    
@FedericoklezCulloca: yes its like that only. so how should I access it ?
– VVVV
4 hours ago
    
@FedericoklezCulloca: see my updated question. I want to compare and access it
– VVVV
4 hours ago

答案:

答案1:

Dont do JSON.stringify(UMSGroupDetailsObj()); since you have already json format

 var LoginUserName = "Nadeem5.khan";
    var UMSGroupDetailsObj = function () { return [{"GroupName":"CMM","GroupID":625},{"GroupName":"Construction_Eng","GroupID":626},{"GroupName":"DY. CTO","GroupID":624},{"GroupName":"Fiber Engineer","GroupID":622},{"GroupName":"Fiber Lead","GroupID":623}]; }
    var UMSGroupDetails = UMSGroupDetailsObj;

    var CurrentGroupName = "Fiber Engineer";
    var CurrentGroupID = "622";
    var UMSLocationDetailsObj = function () { return [{"LocationName":"Kalyan","LocationID":"INMUKLYN01"}]; }
    var UMSLocationDetails = JSON.stringify(UMSLocationDetailsObj())

Also please check json format is correct ?

答案评论:

原文地址:

https://stackoverflow.com/questions/47752174/invalid-operand-to-in-object-expected-using-json-values

添加评论

友情链接:蝴蝶教程