Unkn0wn
Unkn0wn

Reputation: 70

Jquery Input value call array

So I'm trying to make the value inputted to call one of these arrays, however the only method I know is value[0], value[1] however that apparently splits down the value. I normaly do LUA and am not yet used to jQuery.

$(function(){
// User Pets
var John = ['Dog', 'Alive'];
var Emily = ['Cat', 'Alive'];
var Frank = ['Fish', 'Dead'];
var Greg = ['Dog', 'Alive'];
var Hannah = ['Camel', 'Dead'];


$('button').on('click', function() {
var value = $('#name').val()

console.log(value[0], value[1])

}); // Button
}); // Function

Upvotes: 0

Views: 35

Answers (1)

Ram
Ram

Reputation: 144669

You can use an object:

var people = {
 John: ['Dog', 'Alive'],
 Emily: ['Cat', 'Alive'],
 Frank: ['Fish', 'Dead'],
 Greg: ['Dog', 'Alive'],
 Hannah: ['Camel', 'Dead']
}

$('button').on('click', function() {
   var value = $('#name').val();
   var person = people[value];
   if (!person) return console.log('User not found!');
   console.log(person[0], person[1]);
});

Upvotes: 1

Related Questions