0

I am attempting to access one of the object propertie through a bracket notation, but I can not figure out why it is not working. As always, Your help is greatly appreciated!

js:

    var dude = [
      {
        question1: "What is dude's favourite food?",
        choices1: ["Pizza", "Pasta", "Chips", "Ice cream"],
        answer1: 1

      },
      {
        question2: "What was dude's first ever job?",
        choices2: ["Staples", "Vodafone", "Costa", "Post office"],
        answer2: 0
      },

    ]

var counter = 1;

var currentQues = "question"+counter;

console.log(dude[currentQues]);

The console returns "undefined". Is this because it can not access a property through a variable(currentQues), which essentially holds another variable(counter)?

As always, your help is greatly appreciated. I am just a beginner looking to advance.

3
  • dude is an array. So properties should be "0", "1", ... Commented Oct 22, 2017 at 9:05
  • In you case you are trying to access array using string property which is not index, you should use numeric index in your array and only then, when you get object from it, access object property as a string property name.
    – Krusader
    Commented Oct 22, 2017 at 9:06
  • No, no and no. The position in the array gives ypu already the question id. Sth like question1 makes your data harder to parse, not only for people trying to work with it but also for the js parser. Why not simply question Commented Oct 22, 2017 at 9:07

2 Answers 2

2

dude refers to an array of objects. You're trying to access question1 on dude, but you need to access it on dude[0]:

console.log(dude[0][currentQues]);
// -------------^^^

Live Example:

var dude = [
      {
        question1: "What is dude's favourite food?",
        choices1: ["Pizza", "Pasta", "Chips", "Ice cream"],
        answer1: 1

      },
      {
        question2: "What was dude's first ever job?",
        choices2: ["Staples", "Vodafone", "Costa", "Post office"],
        answer2: 0
      },

    ]

var counter = 1;

var currentQues = "question"+counter;

console.log(dude[0][currentQues]);


I would strongly recommend reconsidering that data structure. The names of the properties of the objects should be the same for all the objects, with just their position in the array differentiating them:

var dude = [{
    question: "What is dude's favourite food?",
    choices: ["Pizza", "Pasta", "Chips", "Ice cream"],
    answer: 1
  },
  {
    question: "What was dude's first ever job?",
    choices: ["Staples", "Vodafone", "Costa", "Post office"],
    answer: 0
  },
]

dude.forEach(function(entry, index) {
  var num = index + 1;
  console.log("Question #" + num + ": " + entry.question);
});

1
  • 1
    adjusted upon your advcice, T.J. Many thanks for the input.
    – jimmy118
    Commented Oct 22, 2017 at 9:31
1

You need to take the index of the array as well.

var dude = [{ question1: "What is dude's favourite food?", choices1: ["Pizza", "Pasta", "Chips", "Ice cream"], answer1: 1 }, { question2: "What was dude's first ever job?", choices2: ["Staples", "Vodafone", "Costa", "Post office"], answer2: 0 }],
    counter = 1;
    currentQues = "question" + counter;

console.log(dude[counter - 1][currentQues]);
//               ^^^^^^^^^^^

1
  • Thank you for the response Nina :)
    – jimmy118
    Commented Oct 22, 2017 at 9:10

Your Answer

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

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