I really need your help.
I'd like to structure and build an array like the below.:
var provinces = [
['Ontario','ON'],
['Quebec','QC'],
['British Columbia','BC'],
['Saskatchewan','SK']
];
then, id like to compare the value (x) against my array, ie:
var x = 'Ontario'
if (x matches the value in the array list 'provinces') { then let x = ON }
How do you write something like this in javascript?
Much thanks and appreciation for all your help,
Use the .filter()
function, that receives a true/false return condition to retrieve itens from an array:
var provinces = [ ['Ontario','ON'], ['Quebec','QC'], ['British Columbia','BC'], ['Saskatchewan','SK'] ]; var x = "Ontario"; //Find if any array item matches the word var result = provinces.filter(function(item) { return item[0] == x; }); //If there's a match, get the second index from the first result from the filter if(result.length > 0) x = result[0][1]; alert(x);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.