admin管理员组文章数量:1326123
I'm trying to simplify many if then statements into just one if statement with 2 arrays.
So if I have this:
if (city == 'Atlanta'){
jQuery('#cit').HTML('Atlanta');
jQuery('#pho').HTML('555-555-5555');
}
else if (city == 'Portland'){
jQuery('#cit').HTML('Portland');
jQuery('#pho').HTML('666-666-6666');
}
else if (city == 'StLouis'){
jQuery('#cit').HTML('St Louis');
jQuery('#pho').HTML('777-777-7777');
}
I'm trying to simplify it so that it's just in two or three arrays with only one if statement. Such as [Atlanta, Portland, StLouis], [Atlanta, Portland, St Louis], [555-555-5555, 666-666-6666, 777-777-7777] that way it's easier to edit in the future, plus it would be editable by someone without good knowledge of JS.
Any ideas how to achieve such a thing?
I'm trying to simplify many if then statements into just one if statement with 2 arrays.
So if I have this:
if (city == 'Atlanta'){
jQuery('#cit').HTML('Atlanta');
jQuery('#pho').HTML('555-555-5555');
}
else if (city == 'Portland'){
jQuery('#cit').HTML('Portland');
jQuery('#pho').HTML('666-666-6666');
}
else if (city == 'StLouis'){
jQuery('#cit').HTML('St Louis');
jQuery('#pho').HTML('777-777-7777');
}
I'm trying to simplify it so that it's just in two or three arrays with only one if statement. Such as [Atlanta, Portland, StLouis], [Atlanta, Portland, St Louis], [555-555-5555, 666-666-6666, 777-777-7777] that way it's easier to edit in the future, plus it would be editable by someone without good knowledge of JS.
Any ideas how to achieve such a thing?
Share Improve this question asked Jun 11, 2013 at 16:19 zenzen 1,1653 gold badges30 silver badges54 bronze badges 1-
What arrays are you referring to? Are you looking for a
switch(expression) ... case...
statement? – km6zla Commented Jun 11, 2013 at 16:22
5 Answers
Reset to default 9Store the data in an object with the city
as the key and an array as the value, which hold the real name and phone number:
var data = {
'Atlanta': ['Atlanta', '555-555-555'],
'Portland': ['Portland', '666-666-6666'],
'StLouis': ['St Louis', '777-777-7777'],
// ...
};
jQuery('#cit').HTML(data[city][0]);
jQuery('#pho').HTML(data[city][1]);
Use Json to store your data like thie
var cities = {
Atlanta:{name:'Atlanta',pho:'555-555-5555'},
Portland:{name:'Portland',pho:'333-333-333'}
}
then you can fetch the value like this:
jQuery('#cit').HTML(cities['Atlanta'].name);
Create an object and refer to it, without having to use loops and conditionals:
var cities = {
Atlanta: {
name: 'Atlanta',
phone: '555-555-5555'
},
Portland: {
name: 'Portland',
phone: '666-666-6666'
},
StLouis: {
name: 'St. Louis',
phone: '777-777-7777'
}
};
$('#cit').text(cities[city].name);
$('#pho').text(cities[city].phone);
instead of using an if statement consider using a switch like so
jQuery('#cit').HTML(city);
switch(city)
{
case"Atlanta":
jQuery('#pho').HTML('555-555-5555');
break;
case"Portland":
jQuery('#pho').HTML('666-666-6666');
break;
case"StLouis":
jQuery('#pho').HTML('777-777-7777');
break;
}
The person editing would just need to add, remove and edit the 'case's in the switch. there is no limit to amount of cases you can have in a switch.
Put the cities into an array of object literals, then you can loop through the array and populate the page elements with data from the object.
var cities = [
{code: 'Atlanta', name: 'Atlanta', phone: '555-555-5555'},
{code: 'Portland', name: 'Portland', phone: '666-666-6666'},
{code: 'StLouis', name: 'St Louis', phone: '777-777-7777'},
];
for(var i = 0, l = cities.length; i < l; i++){
if(cities[i].code===city) {
jQuery('#cit').HTML(cities[i].name);
jQuery('#pho').HTML(cities[i].phone);
}
}
本文标签: javascriptUsing two arrays in an if then statementStack Overflow
版权声明:本文标题:javascript - Using two arrays in an if then statement - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742191852a2430363.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论