goolx goolx - 4 months ago 7
Javascript Question

How to set key as variable?

I'm trying to set key as variable. How to do this? Here is a example but not working.



var key = 123456789;
var set[key] = true;
$('.show').text(set[key]);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show"></div>




Answer

you havent defined set as a Set Mozilla docs link to Set

var key = Math.floor(Math.random()*1000000000) + 1000000000;
var set = new Set;
set[key] = true;
$('.show').text(set[key]);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="show"></div>