Skip to content

Instantly share code, notes, and snippets.

@jsgao0
Last active March 13, 2017 05:57
Show Gist options
  • Save jsgao0/12d2c9d3c413a33aef4d52b458352370 to your computer and use it in GitHub Desktop.
Save jsgao0/12d2c9d3c413a33aef4d52b458352370 to your computer and use it in GitHub Desktop.
Object.leftJoin() implements a function to join 2 objects x and y based on the properties of x. However, if the properties of y are not shown in x then skip copy.
var x = {a:1, b:2 };
var y = {a:5, c:3, d:5};
Object.leftJoin = function(leftObj, rightObj) {
var acceptKeyList = Object.keys(leftObj);
var returnObj = {};
for(var key of Object.keys(rightObj)) {
if(acceptKeyList.indexOf(key) < 0) {
Object.defineProperty(rightObj, key, {
enumerable: false
});
}
}
return Object.assign(returnObj, leftObj, rightObj);
}
console.log(Object.leftJoin(x, y));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment