0

I have a third party function which returns an object

abc.xyz() --> this returns an object containing many objects,strings,boolean & arrays . Basically JSON style 
object. 

I am iterating through this object to get only the objects inside this object & that object should have a key named "apple" . Once I locate that object, I'm putting the key into a variable named "index" & then using that variable "index" to get the object that I want using

abc.xyz().index // This should ideally return an object, but this is undefined. Why?

My code below.

var pa= abc.xyz();
    var index; 
    for(var key in pa){
        if (pa.hasOwnProperty(key)) {
            var obj = pa[key];

            for(var prop in obj){
                if(obj.hasOwnProperty(prop)){
                    if(typeof obj === 'object'){ 
                        if( prop == "apple"){
                            index = key;
                        }
                    }
                }
            }
         }
    } 

    el.appendChild(ul(pa.index)); // though I get the correct index when i  
        console.log(index) but why is pa.index undefined? 

If I don't use index variable & I directly say pa.k where k is the value of index , it works. then why wont pa.index work?

Lee
  • 13,462
  • 1
  • 32
  • 45
  • no such thing as a "json object" in javascript. there's JS objects, and then there's json STRINGS which can represent js objects. Use `console.log(abc)` to find out exactly what's in your ojbect. – Marc B May 12 '14 at 17:30
  • 4
    Because `.index` tries to access the `index` key, just like `.k` tries to access the `k` key. For dynamic keys, you use `object[keyVariable]`. See http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets Also, it seems like you're comparing against a constant key - why not just use that constant? – Zirak May 12 '14 at 17:32
  • pa.[index] does not work. even if I use key directly, it gives me the same result – user3621199 May 12 '14 at 17:35
  • its a third party method. I have not defined it. I'm just trying to access the properties inside the abc.xyz() object – user3621199 May 12 '14 at 17:36
  • `pa[index]` not `pa.[index]` – Lee May 12 '14 at 17:37
  • Two things: 1) It's not `obj.[index]`, drop the dot. 2) If the key is in a nested object, of course it won't work, you'll need to traverse the entire chain. See http://liveweave.com/0eznoo (open your console) – Zirak May 12 '14 at 17:39

2 Answers2

0

pa.index looks for the key named "index"

To look up the key for the value of index, you need to use pa[index] - no period, just the brackets.

Collin Grady
  • 2,226
  • 1
  • 14
  • 14
0

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));
Lee
  • 13,462
  • 1
  • 32
  • 45