0

In the following exampmle I'm setting an array of objects and sort them by a key parameter (position). I then need to output the result - but how? See the comment in the code.

$(document).ready(function () {

    function sortHooks(){

        // add modules
        var modules = [ 
            { module: 'navbar', hook: 'hook-header', position: '2'},
            { module: 'logo', hook: 'hook-header', position: '3'},
            { module: 'description', hook: 'hook-header', position: '1'}
        ];

        // sort by "position" ASC
        function byPosition(a,b) {
            if (a.position < b.position)
                return -1;
            if (a.position > b.position)
                return 1;
            return 0;
        }

        // jQuery function with the given parameters
        /*  for each modules as modules
            for each hook as hook
            do the following:
            $( '#' + module )prependTo( '#' + hook )

            so for the given example it should return
            $('#description')prependTo('hook-header')
            $('#navbar')prependTo('hook-header')
            $('#logo')prependTo('hook-header')

            so they can be executed in that specific order.
            And since we're using "prepend" that will mean
            that the items be placed like this:
            #logo
            #navbar
            #description
        */

        return ???
    }

    // Execute sortHooks function
    sortHooks();
});

Maybe something like this, but I'm not exactly sure:

$.each(modules, function(index, value) {
    $( '#' + value.module ).prependTo( '#' + value.hook ));
});

I've tried a bunch of other things as well, one of them being Hook elements with insertAfter, but stay in parent - jQuery , which worked perfectly, but if the position was higher than the children inside the parent it didn't know what to do with it and causing a lot of unwanted behavior.

1
  • 1
    Off-topic: you could use function byPosition(a,b) { return a.position-b.position; } Commented Mar 20, 2014 at 19:06

2 Answers 2

1

just use a regular loop

for(var i=0; i<modules.length; i++){
   $( '#' + modules[i].module )prependTo( '#' + modules[i].hook );
}
Sign up to request clarification or add additional context in comments.

3 Comments

This looks really simple and clean. But how to return the output from the loop to the function? jsfiddle.net/NicotineLL/5HFVY
return what output? there is no output, you are just prepending one element to another
It wasn't working so I assumed I've to output the the result from the "for" loop and send it to the functions "return". Now that you said that I double checked everything else just to find that my elements ids differ... I changed them and everything works fine! jsfiddle.net/NicotineLL/5HFVY/1 Thank you!
1
function byPosition(a,b) { return a.position-b.position; }
modules.sort(byPosition);
modules.forEach(function(obj){
    $('#'+obj.madule).prependTo('#'+obj.hook);
});

3 Comments

note that unless they are using a shim/polyfill, forEach is not available on IE<9
I'm not focusing on IE<9 support, but it's still better to be in there.
@СърГеоргиДемирев $.each could be used instead.

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.