0

I have a global object abc with the following structure

abc = {
    dir_content: {
        dir: [
            "hi"
        ]
        files: [
            "1.txt",
            "2.txt"
        ]
    }
    directory: "greeting"
}

hope I got the notations correct.

dir_content has its values passed from a JSON object by abc.dir_content = data;

I have a function as below

function show_dir() {
    console.log(abc.dir_content.dir);
    console.log(abc.directory);
}

I am expecting console.log(abc.dir_content.dir) to show hi. But its saying its undefined instead. console.log(abc.directory) shows greeting just fine.

Adding: I can print the correct results in the console with console.log(abc.dir_content.dir) . But it says undefined when called in the function.

I need to loop through the arrays of dir and files in the function. But now I stuck at getting js to read the values in the function.

Edit: I found something wrong this my global object declaration. fiddle link http://jsfiddle.net/xh5YH/ . Whats wrong with the anonymous function declaration?

3
  • 1
    Your notation actually does not make much sense. Is dir supposed to be an array? Commented Nov 18, 2011 at 11:05
  • This obviously means that the value for abc.dir_content is not set. try logging abc.dir_content to see if the values from the JSON object are set Commented Nov 18, 2011 at 11:07
  • ok..i'm bad at notations. anyway, abc is a global object. dir_content is a property of abc with a JSON object with arrays namely dir and files. adding...i can read the global object without problem in the console. however, the same commands passed in the function is returning error. Commented Nov 18, 2011 at 11:16

2 Answers 2

1
abc = {
    dir_content: {
        // array of one dir
        dir: [ "hi" ], 
        // array of two files
        files: [
            "1.txt",
            "2.txt"
        ]
    },
    directory: "greeting"
}
Sign up to request clarification or add additional context in comments.

1 Comment

upvoted, but that doesn't really answer my question. had put up a fiddle link at the bottom of my original post. i'm not sure why anonymous function to declare the global object doesn't work.
0
dir_content: {
    dir: {
        key: "hi"
    },
    files: {
        key1: "1.txt",
        key2: "2.txt"
    }
}

Notice the difference between [], () and {}

[] means you are creating an array, but you need an Object, hence the {} brackets. Also, the returned dir_content is not structured correctly. An Object is always structured like this:

{ key: 'content' }

That is why I've added key, key1 and key 2 to the returned data. If you manage to structure the data correctly, everything will work.

3 Comments

Why the downvote? To access the contents of the returned data, it needs to be structured like in the code I posted.
dir and files are arrays. ok..let me add in the keys.
If they are arrays, you don't have to add the keys. I just posted the code before I saw your comment about the arrays ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.