Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a Javascript code that uses an array of objects as:

var words = [{"text":"This", "url":"http://google.com/"},
         {"text":"is", "url":"http://bing.com/"},
         {"text":"some", "url":"http://somewhere.com/"},
         {"text":"random", "url":"http://random.org/"},
         {"text":"text", "url":"http://text.com/"},
         {"text":"InCoMobi", "url":"http://incomobi.com/"},
         {"text":"Yahoo", "url":"http://yahoo.com/"}]

Then I use words in the rest of code and everything works fine.

Then I store the data in a JSON file, lets call the file "myfile.json" its content is:

{
"words": [
{"text":"This", "url":"http://google.com/"},
{"text":"is", "url":"http://bing.com/"},
{"text":"random", "url":"http://random.org/"},
{"text":"some", "url":"http://somewhere.com/"},
{"text":"text", "url":"http://text.com/"},
{"text":"InCoMobi", "url":"http://incomobi.com/"},
{"text":"Yahoo", "url":"http://yahoo.com/"}
]
}

I load this file using d3.json as:

d3.json("myfile.json", function(words) {
    console.log(words);  //Log output to console
});

And then I use words the same as before, now my code does not work! What is the difference between the two things and how can I fix the second method that I load the file?

share|improve this question
2  
the 1st words is an array which contains object, the 2nd words is an object that contains a property words which is an array of objects. –  Hatsjoem Apr 20 at 15:12
    
How can I convert the 2nd words to something similar to 1st? –  TJ1 Apr 20 at 15:13
    
Use words.words to get the same result. –  Hatsjoem Apr 20 at 15:14
    
it is a little confusing. Assume instead of { "words": [ ... I had {"X": [... then what would I use? –  TJ1 Apr 20 at 15:17
1  
in that case words.X –  Hatsjoem Apr 20 at 15:18

2 Answers 2

Like I said in my comment "the 1st words is an array which contains object, the 2nd words is an object that contains a property words which is an array of objects".

You want to use the words property of your object, you can do this like the example below.

d3.json("myfile.json", function(result) {
    console.log(result.words); 
});
share|improve this answer
    
I thought it is the same, but when I used what you said my 2nd method is still not working as 1st. –  TJ1 Apr 20 at 15:31

The difference between the two is that the first is an array, whereas the second is an object with one property words containing the the array of objects in the first example.

To convert the second to the same as the first simply do:

d3.json("myfile.json", function(words) {

    words = words.words;
    console.log(words);  //Log output to console
});
share|improve this answer
    
I thought it is the same, but when I used what you said my 2nd method is still not working as 1st. Is there any other difference that you can think of? –  TJ1 Apr 20 at 15:32
1  
This should be working. I suspect your data is not what you say. In the function, show the output of console.log(JSON.stringify(words)). –  MrCode Apr 20 at 15:34
    
Here is what I get: [{"text":"This","url":"http://google.com/"},{"text":"is","url":"http://bing.com‌​/"},{"text":"some","url":"http://somewhere.com/"},{"text":"random","url":"http://‌​random.org/"},{"text":"text","url":"http://text.com/"},{"text":"InCoMobi","url":"‌​http://incomobi.com/"},{"text":"Yahoo","url":"http://yahoo.com/"}] –  TJ1 Apr 20 at 15:39
    
That's exactly the same as your first example. It's an array of objects so either iterate over it, or directly access the objects e.g. words[0].text –  MrCode Apr 20 at 15:46
    
I got a clue: when I do console.log(words); outside of d3.json(... then I get array[0] but when I do it inside I get array[7]. Looks like words is not defined outside of d3.json(... ? –  TJ1 Apr 20 at 15:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.