How do I nest an array of items dividing them by their first value?

timo

I have a div filled with buttons. The value of these buttons represent widht and height of a product. I'm trying to split up the buttons, sorting them by their width, which is the first value in the html.

So this:

<div id="flagSize">
    <button id="0">100x200cm</button>
    <button id="1">100x250cm</button>
    <button id="2">100x300cm</button>
    <button id="3">100x350cm</button>
    <button id="4">100x400cm</button>
    <button id="5">110x300cm</button>
    <button id="6">120x300cm</button>
    <button id="7">120x350cm</button>
    <button id="8">120x400cm</button>
    <button id="9">140x400cm</button>
    <button id="10">150x400cm</button>
</div>

Has to become this:

<div id="flagSize">
    <div class="buttonGroup">
        <button id="0">100x200cm</button>
        <button id="1">100x250cm</button>
        <button id="2">100x300cm</button>
        <button id="3">100x350cm</button>
        <button id="4">100x400cm</button>
    </div>
    <div class="buttonGroup">
        <button id="5">110x300cm</button>
    </div>
    <div class="buttonGroup">
        <button id="6">120x300cm</button>
        <button id="7">120x350cm</button>
        <button id="8">120x400cm</button>
    </div>
    <div class="buttonGroup">
        <button id="9">140x400cm</button>
    </div>
    <div class="buttonGroup">
        <button id="10">150x400cm</button>
    </div>
</div>

To do this I figured I first have to get the content of the button, splitting it at the X. I then put this value in to an array combined with the ID of the element.

var orderSizes = [];

$('#flagSize button').each(function(){
    var widthValue = $(this).html();
    widthValue = widthValue.split('x');
    var orderItem = [widthValue[0],$(this).attr('id')]
    orderSizes.push(orderItem);
});

console.log(orderSizes);

I now have to check the values of this array and if they are the same as other items in the array, put them together in a div with the class buttonGroup. This is the part where I'm stuck.

Any help with this, or a different, better aproach would be greatly appreciated.

Fiddle

Arun P Johny

Try something like

//a cache holder to hold the wrappers to that it can be reused
var wrappers = {};

$('#flagSize button').each(function () {
    var widthValue = $.trim($(this).html());
    widthValue = widthValue.split('x')[0];
    var wrapper = wrappers[widthValue];
    //if the wrapper does not exists create one and add it after the current element
    if (!wrapper) {
        wrappers[widthValue] = wrapper = $('<div class="buttonGroup"></div>').insertAfter(this);
    }
    //move the element to the respective wrapper
    wrapper.append(this)
});

Demo: Fiddle

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I nest an array of items dividing them by their first value?

From Dev

How do I nest a JSON array?

From Dev

Swift - How do I count the items in an array of a specific value?

From Dev

How do I sort a PHP associative array first by value, then by key?

From Dev

How do i get the value of first index in a returned List of array

From Dev

How do I get all the LI UL elements ID value and place them in a JavaScript array?

From Dev

How do i handle this dividing modulus error?

From Dev

How do i handle this dividing modulus error?

From Dev

How do I copy files without searching through them first?

From Dev

How do I nest this for loop?

From Dev

Ruby - How do I sort an array based on the value of nested hash items?

From Dev

Using laravel-menu, how do I get sub-items to nest?

From Dev

I Need to add items to array, not replace them

From Dev

How do I print the first and last value?

From Dev

In Julia, how do I get the index of the first element in a sorted array that exceeds a certain value?

From Dev

How do I sort array of pairs based on the greater value in first or second

From Dev

How do I push individual array items into another array with PHP?

From Dev

How do I filter a typescript array with items in another array

From Dev

How do I determine the number of items I have stored in an array?

From Dev

How do I add values to class objects then put them in an array?

From Dev

After making permutations, how do I add them to an array? [Java]

From Dev

How do i convert my values in an array/object to measure them?

From Dev

How do I add values to class objects then put them in an array?

From Dev

How do I change the color of the pane dividing lines in tmux?

From Dev

How do I count the number of items in an array that start with the highest number?

From Dev

How do I attach links to individual array items?

From Dev

How do I safely remove items from an array in a for loop?

From Dev

How do I make each of the list items in an array clickable?

From Dev

How do I remove/filter specific items from an array?

Related Related

  1. 1

    How do I nest an array of items dividing them by their first value?

  2. 2

    How do I nest a JSON array?

  3. 3

    Swift - How do I count the items in an array of a specific value?

  4. 4

    How do I sort a PHP associative array first by value, then by key?

  5. 5

    How do i get the value of first index in a returned List of array

  6. 6

    How do I get all the LI UL elements ID value and place them in a JavaScript array?

  7. 7

    How do i handle this dividing modulus error?

  8. 8

    How do i handle this dividing modulus error?

  9. 9

    How do I copy files without searching through them first?

  10. 10

    How do I nest this for loop?

  11. 11

    Ruby - How do I sort an array based on the value of nested hash items?

  12. 12

    Using laravel-menu, how do I get sub-items to nest?

  13. 13

    I Need to add items to array, not replace them

  14. 14

    How do I print the first and last value?

  15. 15

    In Julia, how do I get the index of the first element in a sorted array that exceeds a certain value?

  16. 16

    How do I sort array of pairs based on the greater value in first or second

  17. 17

    How do I push individual array items into another array with PHP?

  18. 18

    How do I filter a typescript array with items in another array

  19. 19

    How do I determine the number of items I have stored in an array?

  20. 20

    How do I add values to class objects then put them in an array?

  21. 21

    After making permutations, how do I add them to an array? [Java]

  22. 22

    How do i convert my values in an array/object to measure them?

  23. 23

    How do I add values to class objects then put them in an array?

  24. 24

    How do I change the color of the pane dividing lines in tmux?

  25. 25

    How do I count the number of items in an array that start with the highest number?

  26. 26

    How do I attach links to individual array items?

  27. 27

    How do I safely remove items from an array in a for loop?

  28. 28

    How do I make each of the list items in an array clickable?

  29. 29

    How do I remove/filter specific items from an array?

HotTag

Archive