3

I have the following array:

 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(),

            };

Sometimes it'll find a certain name, sometimes it won't. What I want to do is:

  var numberspear = $(this).closest('tr').children().eq(indexes[spear]).text();

Wich obviously won't work for a unit if it doens't exist. Now, i could obviously check every index seperatly, but that's not really efficient... Is there a way to check every value of my array and, if it doens't exist, remove it from the array?

Thanks

3
  • 5
    That is not an array.. it is a key/value object Commented Jan 26, 2014 at 19:05
  • try indexes['spear'] or indexes.spear instead Commented Jan 26, 2014 at 19:08
  • And for god's sake, please CACHE $("#commands_table th") and use filter()!! Commented Jan 26, 2014 at 19:53

2 Answers 2

2

JsFiddle

for (var key in indexes) {
   if (isNaN(indexes[key]))
      delete(indexes[key]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could automate the filling of the object and just not add the items that are not found..

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;
    }
}

1 Comment

One more question (sorry, I'm not really familliar with JS, Jquery only): var numberofspear = bevelnamen.closest('tr').childeren().eq(index).text(); Could i do this for each index (if it exists) and put in in the same (or a new) array too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.