我有以下数组:
var indexes = {
'spear' : $('#commands_table th:has(img[src*="unit_spear"])').index(),
'sword' : $('#commands_table th:has(img[src*="unit_sword"])').index(),
'axe' : $('#commands_table th:has(img[src*="unit_axe"])').index(),
'archer' : $('#commands_table th:has(img[src*="unit_archer"])').index(),
'spy' : $('#commands_table th:has(img[src*="unit_spy"])').index(),
'light' : $('#commands_table th:has(img[src*="unit_light"])').index(),
'marcher' : $('#commands_table th:has(img[src*="unit_marcher"])').index(),
'heavy' : $('#commands_table th:has(img[src*="unit_heavy"])').index(),
'ram' : $('#commands_table th:has(img[src*="unit_ram"])').index(),
'cata' : $('#commands_table th:has(img[src*="unit_catapult"])').index(),
'noble' : $('#commands_table th:has(img[src*="unit_noble"])').index(),
'knight' : $('#commands_table th:has(img[src*="unit_knight"])').index(),
};
有时它会找到某个名字,有时不会。我想做的是:
var numberspear = $(this).closest('tr').children().eq(indexes[spear]).text();
如果一个单位不存在,那显然不会对它起作用。现在,我显然可以分别检查每个索引,但这并不是很有效......有没有办法检查我的数组的每个值,如果它不存在,则将其从数组中删除?
谢谢
最佳答案
for (var key in indexes) {
if (isNaN(indexes[key]))
delete(indexes[key]);
}
关于javascript - 检查每个数组值是否为 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21367813/