My Structure of data will be like below
var data = {"Questions" : [
{"QID":"Q1","S":"2"},
{"QID":"Q2","T":"12"},
{"QID":"Q3","U":"22"}], // Also please suggest me to improve this to make parsing easier
"Share" : "Yes"
};
for(var item in data.Questions)
{
if(this.QID == "Q2") // this.QID returns me undefined
{
// I want to retrieve the total object i.e
// {"QID":"Q2","T":"12"} should be returned
}
}
Why does this.QID return me undefined? How can I retrieve the total object, i.e. {"QID":"Q2","T":"12"}?
Don’t iterate Arrays using
for .. inloops. Use a regularforloop orArray.forEach()*.That aside,
thisis not going to be an item in your Array. You need:Using a regular
forloop:Or, my preferred method,
Array.forEach():*Older browsers don’t natively support
Array.forEach(), but you can add it in yourself. See the compatibility notes for an implementation you can use in your code for older browsers.