0

I'm trying to use a variable (data.$choice) instead of (data.client_kits_bios), to iterate through the array.

I guessing can't place a string variable right there? What would the option be to replace it? Or is that not possible?

Thanks!

$( document ).ready(function() {

    var $choice = 'client_kits_bios';
    console.log($choice);

    $.ajax({
        url: 'data_marketingLibrary.json',
        dataType: 'json',
        type: 'get',
        cache: false,
        success: function(data) {
            $(data.$choice).each(function(index, value) {
                console.log(value.name);
                console.log(index);
            });
        }
    });
});
  • 4
    You use bracket notation, as in `data[$choice]` .... and that's a duplicate – adeneo Feb 10 '16 at 20:18
  • Ah thank you adeneo! Sorry for the duplicate! – myaddresstoday Feb 10 '16 at 20:22
  • 1
    Sidebar: the convention of prefixing variable names with a dollar sign is generally meant to be an indicator for when the variable contains a jQuery object. So in this case, `var choice` instead of `var $choice` because it's referencing a string not a jQuery object. – Will Feb 10 '16 at 20:22

0 Answers0