Javascript数组-使用变量作为键

Javascript数组-使用变量作为键,javascript,Javascript,我试图检索一个数组值,其中键是一个变量。JSFiddle——在行业输入中输入'apparel'或'books'。JSFIDLE的输出表明返回的值未定义 问题在于var filename=constants.factsheet-如何正确传递factsheet的值以检索相关文件名 JS: 更改: var filename = constants.factsheet; 致: (请注意,常量不是数组,而是对象。) 在JavaScript中,可以通过两种方式访问对象属性:使用点表示法和文字属性名称(co

我试图检索一个数组值,其中键是一个变量。JSFiddle——在行业输入中输入'apparel'或'books'。JSFIDLE的输出表明返回的值未定义

问题在于
var filename=constants.factsheet
-如何正确传递factsheet的值以检索相关文件名

JS:

更改:

var filename = constants.factsheet;
致:

(请注意,
常量
不是数组,而是对象。)

在JavaScript中,可以通过两种方式访问对象属性:使用点表示法和文字属性名称(
constants.apparel
),或使用括号表示法和字符串属性名称(
constants[“apparel”]
)。在第二种情况下,字符串可以是任何表达式的结果,包括变量查找。所以这些都显示了相同的东西:

// Dot notation
console.log(constants.apparel);

// Brackets with string literal
console.log(constants["apparel"]);

// Brackets with concatentation expression
console.log(constants["app" + "arel"]);

// Brackets using a variable
var name = "apparel";
console.log(constants[name]);

// Brackets using the return value of a function
function foo() { return "apparel"; }
console.log(constants[foo()]);

你明白了。

我在代码中以适用于你的方式发布了上述答案

  $(function () {

      var availableIndustries = ["apparel", "books"];

      $("#industry").autocomplete({
          source: availableIndustries
      });

      $("input[type=image]")
          .button()
          .click(function (event) {

          var constants = {
              'apparel': 'apparel.pdf',
                  'books': 'publishing.pdf',
          };

          var factsheet = document.getElementById('industry').value;
          var filename = constants[factsheet];
          $('#factsheet').text('Your factsheet is ' + factsheet);
          $('#file').text('Your filename is ' + filename);

      });
  });

T.J.Crowder关于使用括号语法通过变量键名查找对象的观点是正确的,我只是通过将静态文件对象常量移出事件函数(无需每次都生成该常量)并缓存
#industry
元素引用对事情进行了一些优化

$(function () {

  var industryInput = $('#industry'),
      availableIndustries = ['apparel', 'books'],
      files = {
          'apparel': 'apparel.pdf',
          'books': 'publishing.pdf'
      };

  industryInput.autocomplete({
      source: availableIndustries
  });

  $('input[type=image]').button().click(function (event) {
      var factsheet = industryInput.value;
      $('#factsheet').text('Your factsheet is ' + factsheet);
      $('#file').text('Your filename is ' + files[factsheet]);
  });
});

非常感谢您的补充解释,非常感谢TJ。
  $(function () {

      var availableIndustries = ["apparel", "books"];

      $("#industry").autocomplete({
          source: availableIndustries
      });

      $("input[type=image]")
          .button()
          .click(function (event) {

          var constants = {
              'apparel': 'apparel.pdf',
                  'books': 'publishing.pdf',
          };

          var factsheet = document.getElementById('industry').value;
          var filename = constants[factsheet];
          $('#factsheet').text('Your factsheet is ' + factsheet);
          $('#file').text('Your filename is ' + filename);

      });
  });
$(function () {

  var industryInput = $('#industry'),
      availableIndustries = ['apparel', 'books'],
      files = {
          'apparel': 'apparel.pdf',
          'books': 'publishing.pdf'
      };

  industryInput.autocomplete({
      source: availableIndustries
  });

  $('input[type=image]').button().click(function (event) {
      var factsheet = industryInput.value;
      $('#factsheet').text('Your factsheet is ' + factsheet);
      $('#file').text('Your filename is ' + files[factsheet]);
  });
});