0

I am new to programming and I am trying to learn node.js and CoffeeScript. I have read a few books and watched some screencasts. And now I have started to code. Now I faced my first problem and was not able to solve it with Google. Already lost a few hours and I am stuck. Maybe someone can give me a light. Here is the problem. I have this json file:

{
  "title": "title",  
  "pages": [ 
    { "name": "Page1", "url": "#Page1", "class": "class", "template":"templateName" }, 
    { "name": "Page2", "url": "#Page2", "class": "class", "template":"templateName" },
    { "name": "Page3", "url": "#Page3", "class": "class", "template":"templateName" }, 
    { "name": "Page4", "url": "#Page4", "class": "class", "template":"templateName" }, 
    { "name": "Page5", "url": "#Page5", "class": "class", "template":"templateName" } 
  ]
}

and the code to get the json file is

configFile = require(file.json)

If I do

console.log(configFile.pages)

I can get the correct information.

But if I do

console.log(configFile.pages.template[0])

I get an undefined error.

Can anyone give me a hand?

2 Answers 2

3
configFile.pages[0].template

it's your structure :)

Sign up to request clarification or add additional context in comments.

Comments

1

template is not an array, pages is. So use this:

console.log(configFile.pages[0].template);

Comments

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.