如果数组在javascript中具有相同的id,如何合并数组中所有属性的两个对象?
JSON:(我在数组中有两个相同的对象属性。我想合并这个数组的所有属性值。我添加了另一个case,如果id被更改,它应该给出如下结果。) 预期结果:(更新了我的问题) 你能试试这个吗如果数组在javascript中具有相同的id,如何合并数组中所有属性的两个对象?,javascript,Javascript,JSON:(我在数组中有两个相同的对象属性。我想合并这个数组的所有属性值。我添加了另一个case,如果id被更改,它应该给出如下结果。) 预期结果:(更新了我的问题) 你能试试这个吗 const数组=[ {批次id:{id:1,接收数量:30,退回数量:5,剩余数量:15},分配数量:10}, {批次id:{id:1,接收数量:30,退回数量:5,剩余数量:15},分配数量:10}, {批次id:{id:2,接收数量:30,退回数量:5,剩余数量:15},分配数量:10}, ] 函数合并(数组
const数组=[
{批次id:{id:1,接收数量:30,退回数量:5,剩余数量:15},分配数量:10},
{批次id:{id:1,接收数量:30,退回数量:5,剩余数量:15},分配数量:10},
{批次id:{id:2,接收数量:30,退回数量:5,剩余数量:15},分配数量:10},
]
函数合并(数组){
var结果=[];
array.reduce(函数(res,value){
如果(!res[value.Lot\u id.id]){
res[value.Lot\u id.id]={
地段编号:{
id:value.Lot\u id.id,
接收数量:0,
退货数量:0,
剩余数量:0,
},分配数量:0
};
结果推送(res[value.Lot\u id.id])
}
res[value.Lot\u id.id]。数量分配+=value.qty\u分配;
res[value.Lot\u id.id]。Lot\u id.qty\u receive+=value.Lot\u id.qty\u receive;
res[value.Lot\u id.id]。Lot\u id.qty\u return+=value.Lot\u id.qty\u return;
res[value.Lot\u id.id]。Lot\u id.qty\u remain+=value.Lot\u id.qty\u remain;
返回res;
}, {});
返回结果
}
log(合并(数组))代码>您可以查看我针对您的问题的解决方案
const数组=[
{
地段编号:{
id:1,
接收数量:30,
退货数量:5,
剩余数量:15,
},
分配数量:10,
},
{
地段编号:{
id:1,
接收数量:30,
退货数量:5,
剩余数量:15,
},
分配数量:10,
},
{
另一个身份证:{
id:1,
接收数量:30,
退货数量:5,
剩余数量:15,
},
分配数量:10,
},
{
另一个身份证:{
id:1,
接收数量:30,
退货数量:5,
剩余数量:15,
},
分配数量:10,
},
];
常量isObject=(值)=>{
返回(
值的类型==“对象”&&
对象的值实例&&
!(数组的值实例)
);
};
常量结果=数组。减少((acc,obj)=>{
const existingObj=acc.find((accObj)=>{
返回Object.keys(accObj).every(
(键)=>Object.keys(obj).indexOf(键)>-1
);
});
如果(现有对象){
Object.keys(obj.forEach)((key)=>{
if(对象(对象[键]){
const innerObj=obj[key];
const existinginnernobj=existingObj[key];
Object.keys(innerObj).forEach((innerKey)=>{
如果(innerKey!==“id”){
existingInnerObj[innerKey]+=innerObj[innerKey];
}
});
}否则{
现有obj[key]+=obj[key];
}
});
}否则{
加速推力(obj);
}
返回acc;
}, []);
控制台日志(结果)代码>
根据更新的问题更新答案:
const mergeArr = () => {
const newArr = [];
let tempId = 0;
array.forEach((item, index) => {
if (tempId !== item.Lot_id.id) {
tempId = item.Lot_id.id;
newArr.push(item);
} else {
const lastItem = newArr[newArr.length - 1];
const lastLot = lastItem.Lot_id;
const newLot = item.Lot_id;
lastLot.qty_receive += newLot.qty_receive;
lastLot.qty_return += newLot.qty_return;
lastLot.qty_remain += newLot.qty_remain;
lastItem.remaining_qty += item.remaining_qty;
lastItem.qty_allocate += item.qty_allocate;
}
});
return newArr;
}
console.log(mergeArr());
请在此处尝试代码:
试试这个
let result = mergeElements(array);
console.log(result);
/**
* function accepts the array of objects you need to merge by adding fields
* */
function mergeElements(array = []) {
let sumOf = [];
if (array.length <= 1) {
return array;
} else {
sumOf[0] = array[0];
let index = 0;
array.forEach(function (item, index) {
if (index != 0) {
sumOf[0] = iterateItem(item, sumOf[0]);
}
index++;
});
}
return sumOf;
}
/**
*function for indepth iteration of objects
* */
function iterateItem(item, sumOf) {
if (typeof item == "object") {
for (let key in item) {
if (typeof item[key] != "object") {
if (typeof sumOf[key] == "undefined") {
sumOf[key] = 0;
}
if (key == "id") {
continue;
}
sumOf[key] += item[key];
} else {
if (typeof sumOf[key] == "undefined") {
sumOf[key] = {};
}
sumOf[key] = iterateItem(item[key], sumOf[key]);
}
}
} else {
sumOf += item;
}
return sumOf;
}
let result=mergeElements(数组);
控制台日志(结果);
/**
*函数通过添加字段接受需要合并的对象数组
* */
函数mergeElements(数组=[]){
设sumOf=[];
如果(array.length您需要发布您尝试的内容。您的输出是错误的。所有id都是1,因此您只有一个对象,但在您的代码中有两个,它们的id是2,即使没有id,例如2@ğağrı非常感谢您通知该错误。我已经修复了该错误。请您现在检查一下,在这个解决方案中,在执行加法操作之前需要检查lot\u id。如果它的lot\u id发生变化,那么这应该不起作用。我更新了我的答案并添加了一个链接。请注意,网站(repl.it,这是一个很好的工具)目前遇到一些问题,因此如果链接出现问题,请稍后再试。
const reduceArr = array.reduce((total, {Lot_id: {qty_receive, qty_return, qty_remain}, qty_allocate}) => {
total.Lot_id.qty_receive += qty_receive;
total.Lot_id.qty_return += qty_return;
total.Lot_id.qty_remain += qty_remain;
total.qty_allocate += qty_allocate;
return [total];
});
const mergeArr = () => {
const newArr = [];
let tempId = 0;
array.forEach((item, index) => {
if (tempId !== item.Lot_id.id) {
tempId = item.Lot_id.id;
newArr.push(item);
} else {
const lastItem = newArr[newArr.length - 1];
const lastLot = lastItem.Lot_id;
const newLot = item.Lot_id;
lastLot.qty_receive += newLot.qty_receive;
lastLot.qty_return += newLot.qty_return;
lastLot.qty_remain += newLot.qty_remain;
lastItem.remaining_qty += item.remaining_qty;
lastItem.qty_allocate += item.qty_allocate;
}
});
return newArr;
}
console.log(mergeArr());
let result = mergeElements(array);
console.log(result);
/**
* function accepts the array of objects you need to merge by adding fields
* */
function mergeElements(array = []) {
let sumOf = [];
if (array.length <= 1) {
return array;
} else {
sumOf[0] = array[0];
let index = 0;
array.forEach(function (item, index) {
if (index != 0) {
sumOf[0] = iterateItem(item, sumOf[0]);
}
index++;
});
}
return sumOf;
}
/**
*function for indepth iteration of objects
* */
function iterateItem(item, sumOf) {
if (typeof item == "object") {
for (let key in item) {
if (typeof item[key] != "object") {
if (typeof sumOf[key] == "undefined") {
sumOf[key] = 0;
}
if (key == "id") {
continue;
}
sumOf[key] += item[key];
} else {
if (typeof sumOf[key] == "undefined") {
sumOf[key] = {};
}
sumOf[key] = iterateItem(item[key], sumOf[key]);
}
}
} else {
sumOf += item;
}
return sumOf;
}