0

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
2
  • what you want to achieve here...what you mean by call array what do you want to do with value array? Commented Dec 16, 2016 at 2:47
  • if you want to convert it to string you don't need query... you can simply do value.join() Commented Dec 16, 2016 at 2:49

1 Answer 1

1

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]);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.