1

I have an associative array:

var array1 = {
    a1: {
        id: "box1",
        title: "box 1"
    },
    a2: {
        id: "box2",
        title: "box 2"
    },
    a3: {
        id: "box3",
        title: "box 3"
    }
};

I then have another array which has references to the first array:

var array_order = ["a3:positionA", "a2:postitionB", "a1:positionC"];

I want to loop through the first list and then use the second list to find the position text

I am using jQuery so I have

$.each(array1, function(i,o) {
  something in here where I can use i to find out what position. e.g. if a1 I would get positionC
}
1
  • 1
    Possibly irrelevant side note: your array1 variable is not an array. JavaScript only has one kind of arrays: good old zero-based numeric arrays such as array_order. Commented May 18, 2011 at 10:30

3 Answers 3

0

Easier and faster iterate over array_order instead of array1:

for( var i = 0, len = array_order.length; i < len; i++ ) {
    var ref = array_order[i].split(':');

    if( ref.length === 2 && ({}).hasOwnProperty.call( array1, ref[0] ) ) {
        var array1Property = array1[ ref[0] ];
        var array1PropertyPosition = ref[1];
    }
} 

Iteration over array1 properties can be realized also, but it's significantly slower:

for( var prop in array1 )
    if( ({}).hasOwnProperty.call( array1, prop ) )
        for( var i = 0, len = array_order.length; i < len; i++ ) {
            var ref = array_order[i].split(':');
            if( ref.length === 2 && prop === ref[1] ) {
                var array1Property = array1[ prop ];
                var array1PropertyPosition = ref[1];
            }
        }

And there is no need in jQuery.

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

2 Comments

I ended up iterating over array_order like you said
Is there any need to double "=" in ref.length = 2 ?
0

Ok first things first JS calls associative arrays as objects only.

next to iterate over each key in object

for(var prop in array1) {
    if(array1.hasOwnProperty(prop))
        for(key in array_order){
           if( key === prop){
           doSomethingwith(array_order[key]);

           }

        }
}

2 Comments

Iteration over Array using for( in ) are too slow. Try to launch following example in the firebug: gist.github.com/978743
Correction: for( in ) slower versus for( var .. ++ ), except v8, there for( in ) faster due optimizations.
0
var temp_array_order = {};
$.each(array_order, function(key, val) { 
    temp_array_order[val.substr(0,2)] = val.substr(3); 
};
$.each(array1, function(key, val) {
    var category = temp_array_order[key];
    ...
});

8 Comments

jQuery .each too slow versus simple for iteration over array, and your solution are suppose, that array1 properties names length equals 2 chars always.
var category = temp_array_order[key]; there is must be check, that it's temp_array_order own property, not one, which coming from prototypes chain.
@PhillipKovalev meh. The code is smaller. Prototype checking isnt really needed, $.each uses a for loop internally.
I'm sorry, I was wrong about jQuery .each perfomance -- it's best of all. But I disagree with needless of property ownership checking.
@Phillip but it's not needed because $.each uses for (var i = 0; ... i++) It does not use for ... in loop.
|

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.