I have encountered an issue while iterating through receipts in the given code snippet. The objective is to fetch the receipt number for each receipt and add it to a JSON object. However, I am facing a problem where the same receipt is appearing in two separate JSON objects. Can anyone provide assistance with this matter? Thank you in advance.
const shopList = [];
const shop = {
"shopNumber": "2242461",
"shopDate": "2020-06-13T00:00:00.000-05:00",
"shopAmount": "100",
"shopBank": "HDFC",
"shopDetails": {
"shopNumber": "1242461",
"shopDate": "2020-06-13T00:00:00.000-05:00",
"shopBank": "HDFC",
"shopAmount": "100"
},
"receiptDetails": [{
"userNumber": "115729",
"receiptNumber": "temp1"
},
{
"userNumber": "115726",
"receiptNumber": "temp2"
}
]
};
shop.receiptDetails.forEach((receipt) => {
var temp = shop.shopDetails;
temp.receiptNumber = receipt.receiptNumber;
shopList.push({
shopNumber: shop.shopNumber,
userNumber: Number(receipt.userNumber),
receiptNumber: receipt.receiptNumber,
shopDetails: temp,
isSync: 0
});
});
shopList.forEach((receipt) => {
console.log(receipt);
});
The current output shows:
{
shopNumber: '2242461',
userNumber: 115729,
receiptNumber: 'temp1',
shopDetails: {
shopNumber: '1242461',
shopDate: '2020-06-13T00:00:00.000-05:00',
shopBank: 'HDFC',
shopAmount: '100',
receiptNumber: 'temp2' ----**this should be temp1**
},
isSync: 0
} {
shopNumber: '2242461',
userNumber: 115726,
receiptNumber: 'temp2',
shopDetails: {
shopNumber: '1242461',
shopDate: '2020-06-13T00:00:00.000-05:00',
shopBank: 'HDFC',
shopAmount: '100',
receiptNumber: 'temp2'
},
isSync: 0
}