javascript - 检查每个数组值是否为 NaN

标签 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*="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();

如果一个单位不存在,那显然不会对它起作用。现在,我显然可以分别检查每个索引,但这并不是很有效......有没有办法检查我的数组的每个值,如果它不存在,则将其从数组中删除?

谢谢

最佳答案

JsFiddle

for (var key in indexes) {
   if (isNaN(indexes[key]))
      delete(indexes[key]);
}

关于javascript - 检查每个数组值是否为 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21367813/

相关文章:

jQuery 全日历 : contextmenu

java - 从数组创建二叉树

php - 检查 PHP 中解码的 JSON 字符串数组中是否有数字

javascript - 在 Javascript 中创建存储数组,并在每次点击时推送元素 ID

javascript - 如何在组中添加 2+ 层?

javascript - mongodb/mongoose - 按两个字段排序

javascript - 无法使用formData数组一次上传5张图片

javascript - 将部分匹配字符串替换为粗体字符串

javascript - Keyup 来操作 div

javascript - jQuery:找到包含 "Up"的 td 索引,然后使用该索引在下一个 tr 中选择相同的 td ?