Hitmands
Hitmands

Reputation: 14189

Explain expression: Object followed by Array

I saw a javascript expression like this:

var foo = {...}[...];

can anyone explain what that means?

Best Regards

Update:

this is a code example:

var ENV_PRODUCTION = {
  production: true,
  development: false
}[process.ENV.NODE_ENV|| 'development'];

Upvotes: 1

Views: 74

Answers (3)

Jonathan Newton
Jonathan Newton

Reputation: 840

It will return the inner value by key value see:

var foo = {
 "1": "Jam",
"2":"Stuff"}[1];

var bar = {
 "1": "Jam",
"2":"Stuff"}[2];

var foo = {
 "1": "Jam",
"2":"Stuff"}[1];

var bar = {
 "1": "Jam",
"2":"Stuff"}[2];

var jam = {
 "1": "Jam",
"B":"Other Stuff"}['B'];

var foobar = {
 "...": "...",
"B":"Other Stuff"}['...'];

console.log(foo, bar, jam, foobar)

Further documentation

Upvotes: 4

Vladimir M
Vladimir M

Reputation: 4489

Its a reference to element of the object.

Try in console:

var x = {a:1}['a'];

x = 1

Upvotes: 1

gurvinder372
gurvinder372

Reputation: 68413

This is a spread operator as per ES6

The spread syntax allows an expression to be expanded in places where multiple arguments (for function calls) or multiple elements (for array literals) or multiple variables (for destructuring assignment) are expected.

Upvotes: 0

Related Questions