简体   繁体   English

使用 JavaScript 按父数组元素对子数组进行分组

[英]Grouping sub arrays by parent array element with JavaScript

I have a cart page that is loaded from a remote source and I want to change the layout of the page.我有一个从远程源加载的购物车页面,我想更改页面的布局。 I have grabbed all the html(tables) for the cart items and I want to iterate over the items and group them by item name.我已经获取了购物车项目的所有 html(tables),我想遍历这些项目并按项目名称对它们进行分组。 The html on the page is like this:页面上的html是这样的:

<table>
<tbody>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            South Georgia Pocket Tee - Mint / Medium
        </div>
    </td>
    <td class="cart-price">
        $15.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[29209762256]" id="updates_29209762256" value="2" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(29209762256); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $30.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            South Georgia Pocket Tee - Navy / Medium
        </div>
    </td>
    <td class="cart-price">
        $15.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[29209761936]" id="updates_29209761936" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(29209761936); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $15.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            South Georgia Pocket Tee - Navy / Small
        </div>
    </td>
    <td class="cart-price">
        $15.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[29209761872]" id="updates_29209761872" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(29209761872); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $15.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Crimson with Navy / Large
            <br />
            Town/City:
            ohio
            <br>
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[35347518736]" id="updates_35347518736" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(35347518736); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Crimson with Navy / Large
            <br />
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[35347518736]" id="updates_35347518736" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(35347518736); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Crimson with Navy / Small
            <br />
            Town/City:
            ohio
            <br>
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[35345344848]" id="updates_35345344848" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(35345344848); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00

    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Crimson with Navy / Small
            <br />
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[35345344848]" id="updates_35345344848" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(35345344848); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Navy with Red / Medium
            <br />
            Town/City:
            ohio
            <br>
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[35347517136]" id="updates_35347517136" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(35347517136); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Navy with Red / Medium
            <br />
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[35347517136]" id="updates_35347517136" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(35347517136); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Red with Ice Blue / XLarge
            <br />
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[35347521872]" id="updates_35347521872" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(35347521872); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Sage With Ice Blue / XLarge
            <br />
            Town/City:
            ohio
            <br>
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[41628245584]" id="updates_41628245584" value="1" onfocus="this.select();"/>
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(41628245584); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - Sage With Ice Blue / XLarge
            <br />

        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[41628245584]" id="updates_41628245584" value="1" onfocus="this.select();"  />
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(41628245584); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
<tr>
    <td class="cart-item">
        <div class="cart-title">
            Town Specific Short Sleeve Tee - White with Red / Medium
            <br />
        </div>
    </td>
    <td class="cart-price">
        $16.00
    </td>
    <td class="cart-quantity">
        <input type="text" class="cart-qty" size="4" name="updates[35347518416]" id="updates_35347518416" value="1" onfocus="this.select();" />
    </td>
    <td class="cart-remove">
        <a href="#" onclick="remove_item(35347518416); return false;"><i class="icon-trash icon-2x"></i></a>
    </td>
    <td class="cart-total">
        $16.00
    </td>
</tr>
</tbody>

I am using JavaScript to iterate over the `enter code table rows and split the item by 'cart-item'.我正在使用 JavaScript 遍历“输入代码表行”并按“购物车项目”拆分项目。 I am then splitting the string into title, colour, size and town:然后我将字符串拆分为标题、颜色、大小和城镇:

var full_array = new Array();
var item_array = new Array();
var variant_array = new Array();  

$('.cart-item').each(function() {
    var split_string = $(this).find(' .cart-title').html().split(' - ');
    if (split_string[0] != 'remove') {
        item_array['name'] = split_string[0];
        var split_variant = split_string[1].split('/');
        variant_array['colour'] = split_variant[0];
        var split_size = split_variant[1].split('<br>');
        variant_array['size'] = split_size[0];
        variant_array['town'] = split_size[1];
        item_array['value']=variant_array;
        full_array.push(item_array);
        item_array = [];
        variant_array = [];         
    }

This gives me array with elements similar to this:这给了我类似这样的元素的数组:

array{ name:"South Georgia Pocket Tee"
     value:[colour: "Navy",size: "Small", town: undefined]}

What I want to do is group all the parent elements with sub elements under so I can loop them and replace the current cart html with new adjusted html.我想要做的是将所有父元素与子元素组合在一起,这样我就可以循环它们并用新调整的 html 替换当前的购物车 html。 So the cart will look like this:所以购物车看起来像这样:

<div><ul>**Town Specific Short Sleeve Tee**
   <li>Crimson with Navy /Large : 1</li>
   <li>Crimson with Navy /Large(town-London) : 1</li>
   <li>Navy with Red /Medium :1</li>
   <li>Navy with Red /Medium(town-Manchester) : 1</li>
</ul></div>

etc.等。

How would I group the array so I can iterate and replace the html using JavaScript?我将如何对数组进行分组,以便我可以使用 JavaScript 迭代和替换 html?

I sorted this out by changing the original each loop to this我通过将原始的每个循环更改为这个来解决这个问题

        if(split_variant[0].search('<br>')) {
            var split_size = split_variant[1].split('<br>');
            variant_array['size'] = split_size[0];
            variant_array['town'] = split_size[1];
        }else{
            variant_array['size'] = split_variant[1];
        }
        item_array['value']= new Array();
        item_array['value'].push(variant_array);
        full_array.push(item_array);

then I ran this function然后我运行了这个函数

 full_array.forEach(function(value) {
    var existing = output.filter(function(v, i) {
        return v.name == value.name;
    });
    if (existing.length) {
        var existingIndex = output.indexOf(existing[0]);
        output[existingIndex].value = output[existingIndex].value.concat(value.value);
    } else {
        if (typeof value.value == 'string')
            value.value = [value.value];
        output.push(value);
    }
});

and output then had the grouped array, bit messy but does the job.然后输出有分组数组,有点凌乱但可以完成工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将父数组元素和子数组推入javascript中的新数组 - Push parent arrays element and chidren array into new array in javascript 如何在javascript中的子数组数组和子数组数组之间进行过滤? - How to filtering between array of sub arrays and array of sub arrays in javascript? 将 arrays 分组为进一步的子 arrays - Grouping arrays into further sub arrays 使用元素的值及其在JavaScript中重复的次数创建一个子数组数组 - Create an array of sub-arrays with the value of an element and the number of times it has repeated in JavaScript 复制数组数组并修改每个子数组的相同元素 - Replicate Array Of Arrays And Modify Same Element Of Each Sub-Array 通过基于Javascript中第一个数组的元素进行分组来合并两个数组 - Merge two arrays by grouping based on elements of 1st array in Javascript 将基于 arrays 的子数组索引的数组合并为键(NodeJS/Javascript) - Merge array of arrays based sub array index as keys (NodeJS/Javascript) 通过使用在每个子数组的第一个元素中找到的子字符串作为键来组合子数组 - Combine sub-arrays by using as a key a sub-string found in the first element of each sub-array 如何在JavaScript中向数组中的数组添加新元素 - How to add a new element to array in arrays in JavaScript JavaScript:将变量与数组数组中的元素匹配 - JavaScript: match variable to element in array of arrays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM