0

I have a query created dynamically like,

"$select=" + selectedField + "&$orderby=" + selectedField + "" 

This is just the dynamic part of the query where 'selectedField' is a string. Now when I am getting my results (array of objects) and I am trying to get the values within a loop like,

for(var i=0; i<results.length; i++) {
            var opt = results[i].selectedField;
}

opt is undefined. However, if the value of selectedField is 'new_name' and I write

var opt = results[i].new_name;

I get the desired result. Can anyone help me to resolve this?

Priyanka D
  • 15
  • 2
  • 1
    Can you please console.log the results array? – brk Jun 03 '16 at 06:47
  • use `console.log(results);` to see the result. Let us know about the result. – Vahid Najafi Jun 03 '16 at 06:51
  • Read a few docs. You could start here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Property_accessors. This is pretty basic stuff. –  Jun 03 '16 at 06:51

1 Answers1

0

Use var opt = results[i][selectedField];.

Quentin Roy
  • 7,677
  • 2
  • 32
  • 50