Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would break this up into three steps:</p> <ol> <li>Iterate through the source document.</li> <li>Build a representative data structure.</li> <li>Build the output document.</li> </ol> <p>The resulting data structure in step #2 should look something like this:</p> <pre><code>{ '1{4}2{3}6': { name: 'product1', options: { '4': '2', '3': '6' } }, '1{4}3{3}5': { name: 'product1', options: { '4': '3', '3': '5' } }, '2{4}3{3}5': { name: 'product1', options: { '4': '3', '3': '5' } } } </code></pre> <p>To build the structure, try something like this with jQuery:</p> <pre><code>var data = {}; $('#input div').each(function() { var div = $(this); var productId = div.attr('products_id'); var optionId = div.attr('products_options_id'); if (!(productId in data)) data[productId] = { name: div.text(), options: {} }; if (!(optionId in data[productId].options)) { var optionValueId = div.attr('products_options_value_id'); data[productId].options[optionId] = optionValueId; } }); </code></pre> <p>Then, to build the output content, do this:</p> <pre><code>$.each(data, function(productId, product) { var productDiv = $('&lt;div/&gt;').attr('products_id', productId).appendTo('#output'); $('&lt;p/&gt;').text(product.name).appendTo(productDiv); $.each(product.options, function(optionId, optionValueId) { $('&lt;p/&gt;') .text( 'products_options_id_' + optionId + ' : products_options_value_id_' + optionValueId) .appendTo(productDiv); }); }); </code></pre> <p><a href="http://jsfiddle.net/K7UEA/1/" rel="nofollow">Here is a jsFiddle</a> showing the result.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload