Let's say I have a simple array like this:
const arr = {
name: 'Lorem',
age: 'Ipsum',
}
How would I check the value of name based on a variable that is, for example, set on click of a button? Here's what I tried:
const arr = {
name: 'Lorem',
age: 'Ipsum',
}
$('button').click(function(e) {
e.preventDefault();
var key = $(this).attr('data-key');
// this:
console.log(arr.key);
// should return the same result as this:
console.log(arr.name);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button data-key="name">Click Me</button>
arr[key]Also, that is an object, not an array.