Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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