Skip to content

Instantly share code, notes, and snippets.

@paulolorenzobasilio
Created November 12, 2019 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulolorenzobasilio/4d9ea7576b0fdb4efa20712ec307e55d to your computer and use it in GitHub Desktop.
Save paulolorenzobasilio/4d9ea7576b0fdb4efa20712ec307e55d to your computer and use it in GitHub Desktop.
Get subset of a JavaScript object properties

https://stackoverflow.com/questions/17781472/how-to-get-a-subset-of-a-javascript-objects-properties

const object = { a: 5, b: 6, c: 7  };
const picked = (({ a, c }) => ({ a, c }))(object);

console.log(picked); // { a: 5, c: 7 }

This is really just an anonymous function being called instantly. All of this can be found on the Destructuring Assignment page on MDN. Here is an expanded form

let unwrap = ({a, c}) => ({a, c});

let unwrap2 = function({a, c}) { return { a, c }; };

let picked = unwrap({ a: 5, b: 6, c: 7 });

let picked2 = unwrap2({a: 5, b: 6, c: 7})

console.log(picked)
console.log(picked2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment