Associative arrays in jQuery

If I have an associative array like this:

var names = { '10watt' : '_ULR', '20watt' : '_ULR', '30watt' : '_ULR', '40watt' : '_ULR', '50watt' : '_ULR', '60watt' : '_ULR', '75watt' : '_ULR', '100watt' : 'ULCR', '120watt' : 'ULCR', '150watt' : 'ULCR' };

How to write the key value to the browser if the key is this.id?

I tried this, but it did not work (#name is the div that I want to put in the text:

$('#name').text(names(this.id));
+3
source share
1 answer
$('#name').text(names[this.id])

It has nothing to do with jQuery. In JavaScript, if you have an object obj, you can access its property propeither like this obj.propor like this obj["prop"].

+4
source

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


All Articles