Skip to content

Instantly share code, notes, and snippets.

@zeuxisoo
Created March 31, 2021 10:51
Show Gist options
  • Save zeuxisoo/34c6fdd061047aac1ec9f5a5b9f51bab to your computer and use it in GitHub Desktop.
Save zeuxisoo/34c6fdd061047aac1ec9f5a5b9f51bab to your computer and use it in GitHub Desktop.
Update target base on source for testing
// 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