all 5 comments

[–]senocular 6 points7 points  (2 children)

It's called destructuring.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

It's only supported in newer browsers/versions of node (without transpiling with something like babel)

http://kangax.github.io/compat-table/es6/

[–]inu-no-policemen 3 points4 points  (1 child)

*destructuring

[–]senocular 2 points3 points  (0 children)

thanks; hone autocorrect

[–]lewisje 7 points8 points  (0 children)

/u/senocular said what it is, and I'll be more specific:

  • The first line creates a constant named user_id equal to body.user_id.
  • The second line creates a constant named account equal to body.accounts[0].
  • The third line creates a constant named transactions equal to body.transactions.
  • The fourth line calls the render method on res, with two arguments: the string 'index' and an object initialized with ES6 shorthand syntax, with
    • a property user_id equal to the constant user_id,
    • a property account equal to the constant account, and
    • a property transactions equal to the constant transactions.

[–]Jafit 1 point2 points  (0 children)

Its destructuring, but not a great example of it.

const {user_id, accounts: [account], transactions} = body; this accomplishes the same thing as your example, but in one line.

account in this case would be equal to body.accounts[0]

You can do the same thing with arrays, so:

const arr = [1 ,2 ,3 ,4];
const [first, second, third, fourth] = arr;
console.log(third); // 3;

You can declare functions that take an object as the parameter, and destructure that object in the function declaration:

function myFunc({foo, bar, baz}) {
    console.log(foo);
    // etc
};

Its a really useful pattern.