Skip to content

Instantly share code, notes, and snippets.

@bendc
Created June 25, 2018 12:33
Show Gist options
  • Save bendc/ea40569f84cbd4344fe8c106f55e5439 to your computer and use it in GitHub Desktop.
Save bendc/ea40569f84cbd4344fe8c106f55e5439 to your computer and use it in GitHub Desktop.
Unified object assignment
const assign = (target, key, value) =>
Object.assign(target, Object(key) === key ? key : {[key]: value});
@bendc
Copy link
Author

bendc commented Jun 25, 2018

// Example

const object = assign({
  foo: 1
}, "bar", 2);

assign(object, {
  "baz": 3
});

console.log(object); // {foo: 1, bar: 2, baz: 3}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment