My background is in Groovy, which has similar syntax to JavaScript. In Groovy, I can easily copy values from one map to another like this:
def myMap1 = {};
def myMap2 = {};
myMap1["key1"] = "value1";
myMap1["key2"] = "value2";
myMap1["key3"] = "value3";
myMap1.each() { key, value =>
myMap2[key] = value;
}
However, dealing with prototypes in JS has been challenging for me. It seems like accessing key/value pairs without prototyping is possible using Object(), but when trying to update data, I encountered the following issue:
var existingData = {"foo":"thing","bar":"otherThing","baz":"whatever"};
var update = {"foo":"newStuff","bar":"thisChanged","baz":"arghh"};
for (const [ key, value ] of Object.entries(update)) {
console.log("Old value is " + existingData[key]);
existingData[key] = value;
console.log("Setting " + existingData[key] + " to " + value);
}
I expected this code to work, but it resulted in undefined values being displayed in the console. It seems that existingData[key] is not referencing the key correctly. How can I fix this and update the values based on the keys in the second object?