【问题标题】:Sum all values of array objects and merge the objects if inquiry number is same对数组对象的所有值求和,如果查询数相同则合并对象
【发布时间】:2018-08-17 22:23:58
【问题描述】:

我有一个对象,里面是对象数组,我想合并具有相同InquiryNo 的对象,在里面我想添加Quantity(首先将其转换为整数然后添加)并连接@987654323 @ 与', '。我试过这样做,但我得到了错误的总和和一些错误的ProductDesc 值。这是我到目前为止所做的:

for (var i = 0; i < obj.data.length; i++) {
    obj.data[i].Quantity = parseInt(obj.data[i].Quantity);
}

console.log(typeof(obj.data[0].Quantity));

var data = obj.data, result = data.reduce(function (hash) {
    return function (r, a) {
        var key = a.InquiryNo;
        if (!hash[key]) {
            hash[key] = {
                ColorCode: a.ColorCode,
                CustCode: a.CustCode,
                CustomerName: a.CustomerName,
                InquireDate: a.InquireDate,
                InquiryNo: a.InquiryNo,
                LeadTime: a.LeadTime,
                PatternCode: a.PatternCode,
                Post: a.Post,
                PostStatus: a.PostStatus,
                ProductDesc: a.ProductDesc,
                QualityCode: a.QualityCode,
                Quantity: a.Quantity,
                ShipDate: a.ShipDate,
                StyleCode: a.StyleCode,
                Unit: a.Unit
            };
            r.push(hash[key]);
            console.log(hash[key]);
        }
        hash[key].Quantity += a.Quantity;
        return r;
    };
}(Object.create(null)), []);

console.log(result[0].Quantity);

这就是我要与之抗争的对象:

var obj = {
    meta: {
        page: "1",
        pages: "1",
        perpage: "-1",
        total: 114,
        sort: "asc",
        field: "InquiryNo"
    },
    data: [
        {
            InquiryNo: 38090,
            Quantity: "27000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "2/6/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38090,
            Quantity: "27000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "2/6/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Towel"
        },
        {
            InquiryNo: 38090,
            Quantity: "27000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "2/6/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Wash Cloth"
        },
        {
            InquiryNo: 38090,
            Quantity: "27000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "2/6/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Sheett5"
        },
        {
            InquiryNo: 38090,
            Quantity: "27000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "2/6/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Non Terry Kitchen Towel"
        },
        {
            InquiryNo: 38090,
            Quantity: "27000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "2/6/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Window Pane Solid KT"
        },
        {
            InquiryNo: 38303,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38303,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Towel"
        },
        {
            InquiryNo: 38303,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Wash Cloth"
        },
        {
            InquiryNo: 38303,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Sheett5"
        },
        {
            InquiryNo: 38303,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Non Terry Kitchen Towel"
        },
        {
            InquiryNo: 38303,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Window Pane Solid KT"
        },
        {
            InquiryNo: 38304,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38304,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Towel"
        },
        {
            InquiryNo: 38304,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Wash Cloth"
        },
        {
            InquiryNo: 38304,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Sheett5"
        },
        {
            InquiryNo: 38304,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Non Terry Kitchen Towel"
        },
        {
            InquiryNo: 38304,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Window Pane Solid KT"
        },
        {
            InquiryNo: 38305,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38305,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Towel"
        },
        {
            InquiryNo: 38305,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Wash Cloth"
        },
        {
            InquiryNo: 38305,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Sheett5"
        },
        {
            InquiryNo: 38305,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Non Terry Kitchen Towel"
        },
        {
            InquiryNo: 38305,
            Quantity: "10",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "8/1/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Window Pane Solid KT"
        },
        {
            InquiryNo: 38309,
            Quantity: "50000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 27,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "11/27/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38309,
            Quantity: "50000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 27,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "11/27/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Towel"
        },
        {
            InquiryNo: 38309,
            Quantity: "50000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 27,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "11/27/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Wash Cloth"
        },
        {
            InquiryNo: 38309,
            Quantity: "50000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 27,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "11/27/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Bath Sheett5"
        },
        {
            InquiryNo: 38309,
            Quantity: "50000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 27,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "11/27/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Non Terry Kitchen Towel"
        },
        {
            InquiryNo: 38309,
            Quantity: "50000",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 27,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "11/27/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Window Pane Solid KT"
        },
        {
            InquiryNo: 38312,
            Quantity: "1260",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "12/9/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38312,
            Quantity: "1260",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "12/9/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38312,
            Quantity: "1260",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "12/9/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38312,
            Quantity: "1260",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "12/9/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        },
        {
            InquiryNo: 38312,
            Quantity: "473",
            QualityCode: null,
            ColorCode: null,
            PatternCode: null,
            StyleCode: null,
            Post: "U",
            PostStatus: null,
            CustCode: 0,
            Unit: 1,
            CustomerName: "WAL-MART STORES INC.",
            InquireDate: "12/9/2017",
            LeadTime: null,
            ShipDate: null,
            ProductDesc: "Terry Hand Towel"
        }
    ]
};

如果我需要解释其他内容,请告诉我,提前谢谢。

【问题讨论】:

  • 为什么-1i &lt; obj.data.length - 1 中?你想故意省略最后一个元素吗?
  • @connexo 不,我正在检查黑白中的某些内容,这是一个错字...感谢您告诉我。我再次编辑了问题

标签: javascript arrays typescript ecmascript-6 javascript-objects


【解决方案1】:

这就是我能做到的:

let result = [];

obj.data.forEach(function(obj) {
    let id = obj.InquiryNo;
    // converting quantity type from string to integer
    obj.Quantity = parseInt(obj.Quantity);
    if(!this[id]) {
        result.push(this[id] = obj);
    } else {
        this[id].Quantity += parseInt(obj.Quantity);
        this[id].InquireDate += `, ${obj.InquireDate}`;
        this[id].ProductDesc += `, ${obj.ProductDesc}`;
    }
}, Object.create(null));

console.log(result);

【讨论】:

    【解决方案2】:

    我自己可能会使用 for 循环和一些 lodash 实用程序来执行此操作,但您的代码中的一个问题是您总是添加 a.Quantity,即使是新添加的 key。使用else 子句应该可以解决这个问题。

                ..
                console.log(hash[key]);
            } else {
              hash[key].Quantity += a.Quantity;
              // code for ProductDesc 
            }
            return r;
            ..
    

    【讨论】:

      猜你喜欢
      • 2021-10-09
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多