Created
April 10, 2021 20:37
-
-
Save Elijah-trillionz/b198ae5bcde04a85017958fbf0180cc2 to your computer and use it in GitHub Desktop.
Switching values of the same properties in two different objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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