I'm trying to collect all the objects from DATA
and compare to schemesFromPOI
. If the name is AMERICAN_EXPRESS I want to change it to AMEX in the scheme
object only, preserving the DATA
object.
const DATA =
[
{
"name": "MASTERCARD",
"id": "MASTERCARD123"
},
{
"name": "VISA",
"id": "VISA456"
},
{
"name": "AMERICAN_EXPRESS",
"id": "AMEX789"
}
];
const schemesFromPOI = [
{ dpaId: 'DPA1', name: 'MASTERCARD' },
{ dpaId: 'DPA2', name: 'VISA' },
{ name: 'AMERICAN_EXPRESS' }
];
let config = [];
schemesFromPOI.forEach(function (schemeFromPOI) {
let scheme = DATA.find(function (element) { return element.name === schemeFromPOI.name; });
if(scheme) {
if (scheme.name === "AMERICAN_EXPRESS") {
console.log("Before updating scheme.name property: ", DATA);
scheme.name = 'AMEX';
console.log("After updating scheme.name property: ", DATA);
}
config.push(scheme);
}
});
However, when I change scheme.name
, the DATA
object is updated as well. Here is the result of the above console outputs:
Before updating scheme.name property: "[{'name':'MASTERCARD','id':'MASTERCARD123'},{'name':'VISA','id':'VISA456'},{'name':'AMERICAN_EXPRESS','id':'AMEX789'}]"
After updating scheme.name property: "[{'name':'MASTERCARD','id':'MASTERCARD123'},{'name':'VISA','id':'VISA456'},{'name':'AMEX','id':'AMEX789'}]"
Clearly the DATA
object is being updated, not just the scheme
object. Why is this coercion happening? Does it have to do with the .find()
method?