I’m trying to read out the values from a jStorage JSON object called data.shake (val1 .. val6 properties), but cant figure the right syntax out so that it runs correctly. for example the console log output doesnt show the value stored in it.
var sports = {1:'val1',2:'val2',3:'val3',
4:'val4',5:'val5',6:'val6'};
for (sport in sports) {
if ('data.shake.'+sports[sport] != 0) {
console.log(sports[sport]+':'+'data.shake.'+sports[sport])
//output: val1:data.shake.val1
}
}
I think you probably mean this:
You can’t just use a string (e.g.
'data.shake.and expect Javascript to turn this into a variable for you. You’re effectively doing this:Obviously that condition will always pass, because non-empty strings (except
"0") are not equal to0The syntax you need uses the square bracket member operator, which allows you to access an object’s members using the result of an expression.