Created
March 31, 2021 10:51
-
-
Save zeuxisoo/34c6fdd061047aac1ec9f5a5b9f51bab to your computer and use it in GitHub Desktop.
Update target base on source for testing
This file contains hidden or 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
// Data | |
const source = { | |
"name" : "tom", | |
"age" : 12, | |
"sex" : "male", | |
"car" : false, | |
"house" : "" | |
}; | |
const target = { | |
"size" : 30, | |
"money" : 3000, | |
}; | |
const keys = ["name", "age"]; | |
// Function | |
const updateTargetBaseOnSource = (source, target, keys) => { | |
return { | |
...target, | |
...Object.fromEntries( | |
Object | |
.entries(source) | |
.filter(([key, value]) => { | |
return keys.includes(key) || [false, ""].includes(value) | |
}) | |
) | |
} | |
} | |
// Call | |
const changed = updateTargetBaseOnSource(source, target, keys); | |
// Result | |
console.log(changed); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment