var list = new Array(
new Array("HAV", "TWI", "DOG-20::WHCH-2::WCBD-2"),
new Array("TWI", "HAV", "DOG-30::WHCH-2::WCBD-2")
);
function getDog(p1, p2) {
for (var i = 0, l = list.length; i < l; i++) {
if (list[i][0] === p1 && list[i][1] === p2) {
return /^DOG-([0-9]+)::WHCH-[0-9]+::WCBD-[0-9]+$/.exec(list[i][2])[1];
}
}
}
var dog = getDog('HAV', 'TWI'); // 20
var dog1 = getDog('TWI', 'HAV'); // 30
小提琴。
您可以进一步使用第三个参数作为查询来获取其他值:
var list = new Array(
new Array("HAV", "TWI", "DOG-20::WHCH-4::WCBD-7"),
new Array("TWI", "HAV", "DOG-30::WHCH-5::WCBD-6")
);
function getValue(p1, p2, query) {
for (var i = 0, l = list.length; i < l; i++) {
if (list[i][0] === p1 && list[i][1] === p2) {
var regex = new RegExp(query.toUpperCase() + '-([0-9]+)');
return regex.exec(list[i][2])[1];
}
}
}
var dog = getDog('HAV', 'TWI', 'DOG'); // 20
var whch = getDog('HAV', 'TWI', 'WHCH'); // 4
小提琴