• 合并数组里面的数据增加重复数量标记


    var arr=[{"itemId":null,"basicItemId":1228,"itemName":"维他柠檬…10ml","unit":null,"costPrice":null,"typeId":null},
                 {"itemId":null,"basicItemId":1228,"itemName":"维他柠檬…10ml","unit":null,"costPrice":null,"typeId":null},
                 {"itemId":null,"basicItemId":1238,"itemName":"红牛","price":5.98,"icon":"","standard":"250ml","unit":null,"costPrice":null,"typeId":null}]
     
    var items = [];
    function addItem(newItem){
     
        var exists = false;
        for (var i = 0; i < items.length; i++) {
          var item = items[i];
        if (item.basicItemId == newItem.basicItemId) {
       exists = true;
        item.num = item.num+1;
       break;
       }
    }
    if(!exists){
     
    newItem.num = 1;
    items.push(newItem);
    }
     
    }
     
     
    $.each(arr,function(index,val){
       addItem(arr[index])
     
    })
     
    console.info(items);
     
     
     
    整理后:
    function add (arrArr) {
    var shopCarItem = []
    function addItem(newItem, exists = false) { //处理重复数据
    shopCarItem.forEach(item => {
    if (item && item.basicItemId == newItem.basicItemId) {
    exists = true;
    item.num += 1;
    item.totalPrice = item.num * item.price //增加商品总价字段
    }
    });
    if (exists) {
    return
    } else {
    newItem.num = 1;
    newItem.totalPrice = newItem.num * newItem.price //增加商品总价字段
    shopCarItem.push(newItem);
    }
    }
    arrArr.forEach((item, key) => {
    addItem((arrArr[key]))
    })
    console.log(shopCarItem);
    }
    add(arrArr)
     
  • 相关阅读:
    软件工程实践总结作业
    软工实践 产品个人分析
    软工交接情况
    结队第二次作业——WordCount进阶需求
    团队展示(团队)
    软工第一次结对
    软工实践第二次作业
    selenium_04_课堂笔记
    selenium_05_课堂笔记
    selenium_06_课堂笔记
  • 原文地址:https://www.cnblogs.com/zhuwu/p/8116847.html
Copyright © 2020-2023  润新知