0

I have an array:

arr = [ 1, 2 , 3 ]

And another array where i hold DOM elements as

Elem = [ 1, 3]

I need to iterate over arr and only do stuff if the index match. For example since I have elem 1 and 3 when I loop through arr something should only happen for 1 and 3 and 2 should be skipped since there is no elem 2.

Someone told me to look into associative arrays and I wonder how I can do this with the least number of lines.

I want the code to be simple and readable and so far all the examples of associative arrays make no sense and are bloated.

2
  • 4
    So you have DOM elements in the second array, but where the frack are you getting those numbers from ? Commented Apr 26, 2014 at 12:48
  • 1
    "so far all the examples of associative arrays make no sense and are bloated" First off, JavaScript doesn't have associative arrays. It has objects, also sometimes called maps or dictionaries. Here's a non-bloated example: {foo: "bar"}. That defines an object with a property called foo and a value called "bar". You can also do this with strings rather than literals and bracketed notation: var obj = {}; var s = "foo"; obj[s] = "bar"; That creates the same object. Commented Apr 26, 2014 at 12:50

3 Answers 3

1
for(var i = 0;i<arr.length;i++){
if(Elem.indexOf(arr[i])>-1){
//Elem contains arr[i] (contains object that at index i in arr)
//will be called only for 1 and 3 in arr
arr[i] = ... //do what you want with this object.
}
}

Do you mean this?

Sign up to request clarification or add additional context in comments.

1 Comment

@proxyfabio Sorry, confused objc with javascript a bit)) Thanks for edit
1

I modified the second array a bit to allow defining multiple actions in one place. I am not sure if I understand you correctly.

// array of DOM objects available
var arr = ['object1-selector', 'object2-selector', 'object3-selector'];

// array of actions with items that the method should be applied to
var actions = [
    {
       items: ['object1-selector', 'object3-selector'],
       perform: function(elem) {
          alert(elem);
       }
    },
    {
       items: ['object2-selector'],
       perform: function(elem) {
          alert(elem);
       }
    },
    {
       items: ['object4-selector'],
       perform: function(elem) {
          alert(elem);
       }
    }
];

//forEach loop that iterates over actions and checks if selector exists. 
//If yes - it invokes the method
actions.forEach(function(action) {
    action.items.forEach(function(item) {
        if(arr.indexOf(item) > -1) {
              action.perform(item);
        }
    });
});

If you want to have actions defined in one place and objects in a multidimensional array - let me know. I will try to adjust the example. If you don't store selectors but whole DOM objects, just modify the items: array and loop, that checks if element exists.

Oh, and here is jsfiddle: http://jsfiddle.net/3WJxc/2/. jQuery used only for alert() to show you working example.

2 Comments

Notice that do is a keyword and cannot be used as an identifier. Quote it when using it as a property name.
Right, thanks for your vigilance. Already changed that keyword to something better.
1

Not really sure how you identify the elements in the second array but this is my suggestion. Array with ids

var arr = [ "id_1", "id_2", "id_3" ]

var Elem = {
    "id_1": html_element,
    "id_2": html_element,
    "id_3": html_element
}

Then all you need to do is

for( var i = 0; i < arr.length; i++ ) {
    if( Elem[ arr[i] ] ) {
        // do stuff
    }
}

1 Comment

Your Elem is not a function, so why do you call it?

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.