How to access an immediate unknown key in an object

how to access an immediate unknown key in an object. in pic, "367: sl" is a dynamic key, and I want to access the cptyName value, which is located in "367: sl". thanks in advance

pic

+4
source share
2 answers

If your dynamic key is the only key in the object quotes, you can get the first key, and then access it with:

var firstKey = Object.keys(quotes)[0];
var cptyName = quotes[firstKey].cptyName;

http://jsfiddle.net/mGZpa/

+7
source

You can iterate through an object.

var quotes = {
    '367:sl': 'cptyname'
}

for(var i in quotes) {
    alert(quotes[i]);
}

Demo

+1
source

Source: https://habr.com/ru/post/1545198/


All Articles