Javascript 检查每个数组值是否为NaN

Javascript 检查每个数组值是否为NaN,javascript,jquery,arrays,Javascript,Jquery,Arrays,我有以下数组: 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*="uni

我有以下数组:

 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();
如果一个单位不存在,Wich显然是不起作用的。现在,我显然可以单独检查每个索引,但这不是真正有效的。。。是否有方法检查数组的每个值,如果它不存在,则将其从数组中删除

谢谢


您可以自动填充对象,而不添加找不到的项

var items = ['spear', 'sword', 'axe', 'archer',
             'spy', 'light', 'marcher', 'heavy',
             'ram', 'cata', 'noble', 'knight'],
    indexes = {},
    headers = $('#commands_table th');

for (i=var i = 0, len = items.length; i < len; i+++){
    var item = items[i],
        index = headers.filter(':has(img[src*="unit_'+ item +'"])').index();

    if (!isNAN(index)){
        indexes[item] = index;
    }
}
var items=[“矛”、“剑”、“斧”、“弓箭手”,
‘间谍’、‘轻’、‘游行者’、‘重’,
“拉姆”、“卡塔”、“贵族”、“骑士”],
索引={},
headers=$('#命令_表th');
对于(i=var i=0,len=items.length;i
这不是一个数组。。这是一个键/值对象尝试索引['spear']或
索引。spear
而不是。看在上帝的份上,请缓存
$(“#命令_表th”)
并使用
过滤器()!!还有一个问题(很抱歉,我不太熟悉JS,只熟悉Jquery):var numberofspear=bevelnamen.closest('tr').children().eq(index.text();我是否可以对每个索引(如果存在)执行此操作,并将其放入相同(或新)数组中?
var items = ['spear', 'sword', 'axe', 'archer',
             'spy', 'light', 'marcher', 'heavy',
             'ram', 'cata', 'noble', 'knight'],
    indexes = {},
    headers = $('#commands_table th');

for (i=var i = 0, len = items.length; i < len; i+++){
    var item = items[i],
        index = headers.filter(':has(img[src*="unit_'+ item +'"])').index();

    if (!isNAN(index)){
        indexes[item] = index;
    }
}