Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

need help in javascript snippet

From the following snippet i have to iterate through all receipts and get the receipt number and add that receipt object to the json.But i am getting same receipt in two jsons.Where i am missing.Can anyone help?Thanks 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);
});

I am getting output as

{
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
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That is because temp is a reference to shop.shopDetails. Objects in JavaScript are passed by reference, when you assign the temp.receiptNumber, it equals to shop.shopDetails.receiptNumber – Therefore, the temp value that you get in the next iterations is carried over from the last iteration

Read more about how javascript objects variable works in this answer, I think it explains it very well.

As for your approach, one of many ways you could solve it is to clone the object of shop.shopDetails in each iteration, therefore you’ll get a new object instead.

E.g.

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}; // This is to clone an object, there are other ways than this.
    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);
});
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading