Skip to content

Instantly share code, notes, and snippets.

@Elijah-trillionz
Created April 10, 2021 20:37
Show Gist options
  • Save Elijah-trillionz/b198ae5bcde04a85017958fbf0180cc2 to your computer and use it in GitHub Desktop.
Save Elijah-trillionz/b198ae5bcde04a85017958fbf0180cc2 to your computer and use it in GitHub Desktop.
Switching values of the same properties in two different objects
// i still feel like there is a better way to do this, but this is what i came up with. If you know a better way please share
const oldObject = { name: 'John Doe', nationality: 'South Africa' };
const newObject = { name: 'John Doe Seth' };
function changeDataInObjects(newData, oldData) {
for (let i in oldData) {
for (let j in newData) {
if (i === j) { // i and j represents the property names of oldData and newData respectively
oldData[i] = newData[j];
} else {
oldData[j] = newData[j];
}
}
}
return oldData;
}
changeDataInObjects(newObject, oldObject)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment