JavaScript “associative” array access

前端 未结 3 1295
一生所求
一生所求 2021-01-02 01:21

I have a simple simulated array with two elements:

bowl["fruit"] = "apple";
bowl["nuts"] = "brazilian";
3条回答
  •  抹茶落季
    2021-01-02 01:27

    I am not sure I understand you. You can make sure the key is a string like this

    if(!key) {
      return;
    }
    var k = String(key);
    var t = bowl[k];
    

    Or you can check if the key exists:

    if(typeof(bowl[key]) !== 'undefined') {
      var t = bowk[key];
    }
    

    However, I don't think you have posted the non-working code?

提交回复
热议问题