我有一个对象,里面是对象数组,我想合并具有相同InquiryNo
的对象,我希望在其中添加Quantity
(首先将其转换为整数然后添加)并将ProductDesc
与', '
连接起来。我尝试过这样做,但是得到了错误的总和以及一些错误的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"
}
]
};
请提前告知我是否需要解释其他内容。
答案 0 :(得分:1)
我可能会使用for循环和一些lodash utils来做这件事,但代码中的问题是你总是添加a.Quantity
,即使对于新添加的key
也是如此。使用else
子句应该解决这个问题。
..
console.log(hash[key]);
} else {
hash[key].Quantity += a.Quantity;
// code for ProductDesc
}
return r;
..
答案 1 :(得分:0)
这就是我能够做到的:
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);