Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<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>
 

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