I was trying to read all the values in a map/jsonobject. Following is my function call,
a ={"a":{"b":"c"}}
result= test(a,'a')
console.log(result)
And the function is,
function test(jsonObj,name){
main = ""
if(jQuery.type(jsonObj)==='object'){
$.each(Object.keys(jsonObj),function(){
main += test(jsonObj[this],name+'_'+this)
});
}else{
main +=jsonObj;
}
return main
}
When I execute the above script I am getting an ouput c
.
Later I modified the function a little,
function test(jsonObj,name){
main = ""
if(jQuery.type(jsonObj)==='object'){
$.each(Object.keys(jsonObj),function(){
resp = test(jsonObj[this],name+'_'+this) //this line added
main +=resp
});
}else{
main +=jsonObj;
}
return main
}
Now I am getting cccc
as my output. I am not able to understand the behaviour. Why am I getting different(unexpected) result when I introduced a new variable?