1

I am trying to return the values from this array using a function. The desired result in this case is 4, 3, 22. Located in the second column of selection1

var list = 
{"selection1": [

{"answers": [1, 4, 5, 7]}, 
{"answers": [4, 3, 2, 1]},
{"answers": [10, 22, 12, 34]}, 

],

"selection2": [

{"answers": [31, 34, 35, 37]}, 
{"answers": [44, 43, 42, 41]},
{"answers": [20, 42, 22, 54]}, 

]};

and I want to return a desired column by calling

get_column_from_object(1, list, 'selection1') to return [4, 3, 22]

How do I write a function that allows me to return these values from my array?

This is where I got too

var list = {
  "selection1": [{
    "answers": [1, 4, 5, 7]
  }, {
    "answers": [4, 3, 2, 1]
  }, {
    "answers": [10, 22, 12, 34]
  }, ]
};

function get_column_from_object(column_number, array, property) {
  var answer = [];
  for (i = 0; i < array.length; i++) {
    answer.push(array[i][property].answers[column_number]);

  }
  return answer;
}
var new_array = get_column_from_object(1, list, 'selection1');

document.getElementById("demo").innerHTML = new_array;
<p id="demo"></p>
Barmar
  • 669,327
  • 51
  • 454
  • 560
  • 2
    Welcome to Stack Overflow! StackOverflow is not a free coding service. You're expected to [try to solve the problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users). Please update your question to show what you have already tried in a [mcve]. For further information, please see [ask], and take the [tour] :) – Barmar Jun 25 '20 at 23:36
  • Shouldn't you have quotes around `selection1` in the call? – Barmar Jun 25 '20 at 23:36
  • maybe but I'm asking for help - at the moment there is no function written. I'm asking how I write a function... to select these values – DominicBoston Jun 26 '20 at 00:14
  • What part are you having trouble with? How to iterate over an array, how to access an object key from a variable? – Barmar Jun 26 '20 at 00:16
  • If you can't even write the example properly (you don't know the difference between variables and strings), you need to study the language more. – Barmar Jun 26 '20 at 00:17
  • See https://stackoverflow.com/questions/4244896/dynamically-access-object-property for how to access the property dynamically. – Barmar Jun 26 '20 at 00:18
  • Hi Barmar, I've tried to explain myself better now – DominicBoston Jun 26 '20 at 00:21
  • It should be `object[i][array].answers[column_number]` – Barmar Jun 26 '20 at 00:24
  • There's no need to fill the array first. Use `answer.push()` instead of assigning to `answer[i]`. – Barmar Jun 26 '20 at 00:24
  • Your parameter names are very confusing. `object` is an array, not an object. `array` is a property name, not an array. – Barmar Jun 26 '20 at 00:26
  • Apologies for the incorrect terms - I thought my array (as you described) was a JSON object. Shall I edit the post to make these terms less confusing. Thanks for your help already. – DominicBoston Jun 26 '20 at 00:32
  • Editing would be good. – Barmar Jun 26 '20 at 00:33
  • edited: tried to use all correct terms. Thanks again for your help getting this question written correctly. – DominicBoston Jun 26 '20 at 00:50
  • That code looks like it should work. – Barmar Jun 26 '20 at 00:57
  • https://www.w3schools.com/code/tryit.asp?filename=GG6FDDBW51KZ Maybe I'm missing quotations, I tried a few options – DominicBoston Jun 26 '20 at 01:18
  • Like I said earlier, you need quotes around `selection1`: `get_column_from_object(1, list, 'selection1')` – Barmar Jun 26 '20 at 01:21
  • Otherwise it uses it as a variable, which doesn't work because you never assigned it. – Barmar Jun 26 '20 at 01:21
  • apologies, edited but still no result. https://www.w3schools.com/code/tryit.asp?filename=GG6FMQAFF7AW I'll continue to look into it. – DominicBoston Jun 26 '20 at 01:28
  • I misread your object, I thought it was an array. See https://www.w3schools.com/code/tryit.asp?filename=GG6KX0SJVOSS – Barmar Jun 26 '20 at 04:30
  • Thanks Barmar, much appreciated. – DominicBoston Jun 26 '20 at 19:53

1 Answers1

0

Thanks to Barmar for the solution

<p id="demo"></p>

<script>
var list = {"selection1": [{"answers": [1, 4, 5, 7]}, {"answers": [4, 3, 2, 1]},{"answers": [10, 22, 12, 34]}, ]};

function get_column_from_object(column_number, object, property) {
  var answer = [];
  var array = object[property];
   for (i = 0; i < array.length; i++) {
     answer.push(array[i].answers[column_number]);

     }
  return answer;
}


var new_array = get_column_from_object(1, list, 'selection1');
document.getElementById("demo").innerHTML = new_array;

</script>