0

I have the following syntax:

 $.each(result, function(key, value){
         alert(value.first_name);
    });

first_name is obviously a value in the array.

what I want/need to do is something like this:

$.each(result, function(key, value){
var column='first_name';
             alert(value.column);
        });

I want to alert the value in the array that comes from a variable. this obviously fails as it sees column as an array key rather that a variable.

is this possible?

alternatively something like alert(value[2]) will work as well

Thanks,

Smudger
  • 10,451
  • 29
  • 104
  • 179

1 Answers1

2

Use value[column] instead of value.column

var value = {
    first_name: 'John'
  },
  column = 'first_name';

alert(value[column]);

For more about JavaScript object https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects

Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188