【问题标题】:merging duplicates in javascript array合并javascript数组中的重复项
【发布时间】:2015-09-30 21:43:07
【问题描述】:

如果我在 JS 中有一个如下所示的数组

lineitems : [{
    quantity : 1,
    unitPrice : 10.00,
    unitPriceLessTax: 8.33,
    SKU: 'SKU123456',
    productName: 'Blue T-Shirt'
 },
 {
    quantity : 1,
    unitPrice : 10.00,
    unitPriceLessTax: 8.33,
    SKU: 'SKU123456',
    productName: 'Blue T-Shirt'
 },
 {
    quantity : 1,
    unitPrice : 48.00,
    unitPriceLessTax: 40.00,
    SKU: 'SKU78910',
    productName: 'Red Shoes'
 }]

如何将其转换为如下所示

lineitems : [{
    quantity : 2,
    unitPrice : 10.00,
    unitPriceLessTax: 8.33,
    SKU: 'SKU123456',
    productName: 'Blue T-Shirt'
 },
 {
    quantity : 1,
    unitPrice : 48.00,
    unitPriceLessTax: 40.00,
    SKU: 'SKU78910',
    productName: 'Red Shoes'
 }]

基本上希望根据 SKU 合并重复项

【问题讨论】:

  • 请形成您的代码以获得更好的可见性
  • 你想比较什么?

标签: javascript arrays


【解决方案1】:

你可以使用关联数组:

var newLineItems = new Array();
$.each(lineItems, function (index) {
    if (newLineItems[this.SKU])
        newLineItems[this.SKU].quantity += this.quantity;
    else
        newLineItems[this.SKU] = this;
});

【讨论】:

  • 使用对象,而不是数组。
【解决方案2】:

你可以使用 Array.prototype.forEach()

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach

var result = []
var temp = [];
lineitems.forEach(function (element, index, array) {
    if (temp[element.SKU] === undefined)
        temp[element.SKU] = element;
    else
        temp[element.SKU].quantity += element.quantity;
});
for (var items in temp){
    result.push(temp[items]);
}

希望有用

【讨论】:

    【解决方案3】:

    纯 JS;快速计算器;

    <script>
        var lineItems = [{
        quantity : 1,
        unitPrice : 10.00,
        unitPriceLessTax: 8.33,
        SKU: 'SKU123456',
        productName: 'Blue T-Shirt'
     },
     {
        quantity : 1,
        unitPrice : 10.00,
        unitPriceLessTax: 8.33,
        SKU: 'SKU123456',
        productName: 'Blue T-Shirt'
     },
     {
        quantity : 1,
        unitPrice : 48.00,
        unitPriceLessTax: 40.00,
        SKU: 'SKU78910',
        productName: 'Red Shoes'
     }];
    
    var nl =[], i=0;
    var collapse = function ()
    {
        if (lineItems.length<=i) return;
        if (nl[lineItems[i].SKU])
        {
            nl[lineItems[i].SKU].quantity+=lineItems[i].quantity;
        }
        else  nl[lineItems[i].SKU]=lineItems[i];
        i++;
        //lineItems.splice(0,1);
        collapse();
    };
    collapse();
    console.log(nl);
    var newLineItems = Object.keys(nl).map(function (key) {return nl[key]});
    console.log(newLineItems);
    console.log('new line items');
    console.log(lineItems);
    </script>

    【讨论】:

    • 嗨 Leo,你能帮我解决我面临的问题吗?基本上,当我运行代码时,原始数组会被值覆盖,所以我假设 ghtis 是因为我们正在使用“return nl [key]”,我如何才能解决“lineItems”的原始值没有被触及但只有“ nl"
    • 我尝试将 lineItems 分配给另一个数组并使用相同的值,但它仍然会更新原始值。我必须根据新数组验证原始数组。谢谢你的帮助
    • 嗨,Ethan,它改变原始值的原因是由于拼接。现在我修改了代码不拼接,所以它不应该改变原始值,并且也应该获得一些性能,因为我们不再调整数组的大小;
    【解决方案4】:

    使用lodash 是管理收藏的快捷方式:

    result = _.uniq(lineitems, "SKU");
    

    【讨论】:

      猜你喜欢
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多