Get object value error Cannot use 'in' operator to search for' length' in

var s = {
    "a":[{
        "name":"...",
        "dec":".."
    }],
    "b":[{
        "name":"...",
        "dec":".."
    }]  
}

$("-sharpid").click(function(){   
    show("a")
})

function show(key){
    $.each(s+"."+key, function(i,e) {
          ....
    });
}

as above ~ console.log (s.a);
normal output
parameter transfer is incorrect
Cannot use "in" operator to search for" length" in [object Object] .a
solution

Jul.06,2022

well, that's not how it works.
you should want to s.a this call
s+'.'+key has several problems:

1. s is converted to a string [object Object]
2. The key you passed in is the variable a , not the string 'a'
3. Even if the string 'a' is passed in, the final result is [object Object] .a
4. Even if s uses the string 's.a' the last one is just a string, you need eval (' s.a') to compile

.
s['a']//

var key = 'a'
s[key]//

function show(key){
    $.each(s[key], function(i,e) {
          ....
    });
}
show('a')

There are two forms of

dot syntax:

1.obj.name
2.obj['name']

your problem appears in $.each (). Click on the console to enter and report an error in the each method
hoping to help

Menu