Here's an example that illustrates how the two different syntaxes for property access work in javascript:
var o = {
index: 'this is index',
APPLE: 'this is APPLE'
};
var index = 'APPLE';
console.log(o.index); // --> this is index
console.log(o[index]); // --> this is APPLE *
console.log(o['index']); // --> this is index
the one marked with a *
is the one you should be using in this case. This form looks for a property of o
, whose name matches the value contained in the variable index
. The other two forms just look for a property named "index"
. So the final line of your example should be:
el.appendChild(ul(pa[index])); // assuming that `el` and `ul()` were both
// defined appropriately elsewhere in your
// script.
Also... it seems that your logic might be simplified substantially. Consider the following:
var pa = abc.xyz();
var result;
for(var key in pa){
if (pa.hasOwnProperty(key)) {
var obj = pa[key];
if(obj.hasOwnProperty("apple")) {
result = obj;
}
}
}
el.appendChild(ul(result));