0

If the following is my array of question, how can I get the value in the position [0][2][1] by supplying the index values in an array eg:answer = [0, 2, 1].

var question = [ [ ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'] ], [ ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'] ], [ ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'] ] ]; var answer = [0,2,1]; question.get(answer); // Is there a way like this?

Is there way like question.get(answer) or question.get([0, 2, 1])?

3
  • 1
    do you mean question[0][2][1]? Commented Sep 20, 2016 at 20:18
  • No. I think this means feeding in the exact values from the answer. I was looking for a dynamic method. Consider if the answer array is built on user's response. Commented Sep 20, 2016 at 20:22
  • I see now. Look at @adeneo's answer, in that case - it's exactly what you want. Commented Sep 20, 2016 at 20:23

3 Answers 3

1

There's a hard coded way:

question[answer[0]][answer[1]][answer[2]]; 

or for any length of a array or nested array:

 var question = [ [ ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'] ], [ ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'] ], [ ['x', 'x', 'x'], ['x', 'x', 'x'], ['x', 'x', 'x'] ] ]; var answer = [0,2,1]; var getanswer= function(answerinput,questioninput){ var val = questioninput; answerinput.forEach(function(item){ val = val[item]; }); return val; } console.log(getanswer(answer,question)); 
Sign up to request clarification or add additional context in comments.

2 Comments

Dynamic part is perfect. I wished there was a way to achieve this without running a loop. I do not own reputation to upvote. Will do future. Thank you.
@OwthamanRooben I think the only way not to have a loop would be to hard code it, also I believe you can accept the answer if you can't upvote. I'm glad this helped you!
0

Let's have some fun...

Object.prototype.getNestedValue = function(...a) { return a.length > 1 ? (this[a[0]] !== void 0 && this[a[0]].getNestedValue(...a.slice(1))) : this[a[0]]; }; var question = [ [ ['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'] ], [ ['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i'] ], [ [':', ',', '?'], ['#', '$', '%'], ['+', '!', '&'] ] ]; console.log(question.getNestedValue(...[0,2,1])); console.log(question.getNestedValue(...[1,2,0])); console.log(question.getNestedValue(...[2,0,1]));

Comments

0

You could use Array#reduce, because you can use the question array as input and get the value as result by iterating the given answer array.

var question = [[['000', '001', '002'], ['010', '011', '012'], ['020', '021', '022']], [['100', '101', '102'], ['110', '111', '112'], ['120', '121', '122']], [['200', '201', '202'], ['210', '211', '212'], ['220', '221', '222']]], answer = [0, 2, 1], getItem = function (array, path) { return path.reduce(function (a, p) { return a[p]; }, array); }; console.log(getItem(question, answer));

ES6

var question = [[['000', '001', '002'], ['010', '011', '012'], ['020', '021', '022']], [['100', '101', '102'], ['110', '111', '112'], ['120', '121', '122']], [['200', '201', '202'], ['210', '211', '212'], ['220', '221', '222']]], answer = [0, 2, 1], getItem = (array, path) => path.reduce((a, p) => a[p], array); console.log(getItem(question, answer));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.