A
A
Arseniy Sokolovsky2021-01-09 22:50:56
JavaScript
Arseniy Sokolovsky, 2021-01-09 22:50:56

Why doesn't it find the map in the array?

There is a code:

function ss() {
    var data = [{
  "24KALT": {
    "md5": "f6efd002536cc08a8fe09d3756e1d625",
    "storageType": 0,
    "url": "",
    "version": 2
  },
  "24K": {
    "md5": "a913b583f0c5b7a2966c62d6e315b3e6",
    "storageType": 0,
    "url": "",
    "version": 2
  }
}];
var val = data.shift();
var kk = document.getElementById('search').value;
 alert(val.kk.version);
}

When I enter for example 24KALT into the input and press the button for executing the ss () function,
then, according to the idea, it should display the version of this card, but instead the error "version is undefined" in the console, what's wrong with me? What to do?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Ezhgurov, 2021-01-09
@SokolarGm

alert(val[kk].version);
If val.kk.version, then the reference will be to the field with the name "kk", and not to the field whose name is in the kk variable.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question