0
var arr = { first: 123, second: 456 };

console.log(arr['first']);            // 123
console.log(arr['second']);           // 456
console.log(arr.first);               // 123
console.log(arr['first', 'second']);  // 456

Problem 1: Why arr['first', 'second'] will get last element?

Problem 2: What's different ['child'] and .child ?


Thx all!

6
  • stackoverflow.com/questions/26017571/… Commented Jan 19, 2015 at 8:56
  • 1
    first, yout arr not an array, also see about Property accessors and comma operator Commented Jan 19, 2015 at 8:58
  • The two questions are both duplicates, but separate ones. Commented Jan 19, 2015 at 8:58
  • sorry @Grundy, I modify title now. Commented Jan 19, 2015 at 9:00
  • @Salmon what output you expect in this case console.log(arr['first', 'second']); :-) Commented Jan 19, 2015 at 9:03

2 Answers 2

3

In JS, objects are collections of key-value pairs (maps or dictionaries, if you want). An array is just another type of object. Your example however features a plain object.

Objects, as I said earlier, have keys, which are used to provide access to methods and properties.

var o = { 
  x: 12, 
  y: function () { console.log(this.x); } 
};

o.y(); // 12

You can access object's members via 2 notations:

  • dot notation: o.x, o,y
  • brackets notation: o['x'], o['y']

Note, in the second approach you must provide a string, which means var name = 'x'; o[name] will work, while o[x] will not.

This is valid for arrays as well:

var arr = ['a', 'b', 'c'];
arr[0] // 'a'
arr['0'] // 'a'
Object.keys(arr) // ['0', '1', '2']

however, you can not do arr.0

Now, regarding your problem: arr['first', 'second'] is equivalent with arr['second'] because you use the comma operator which evaluates each expression, from left to right, and returns the value of the most right one. eg: 1, alert(0), 5 gives you 5

Sign up to request clarification or add additional context in comments.

Comments

3

It's not an array but an object, but the first problem is actually interesting.

Getting object properties using [] notations means you need to pass a key - it may look like an array literal, but it isn't.

What you put in the square brackets is a statement.

For example you could do something like this: arr[ condition ? 'first' : 'second' ] - the statement is parsed and its return value is passed as the key.

In arr['first', 'second'] the inner statement can be read as ('first', 'second') which returns second, because in JS a statement consisting of many of expressions returns the value of the last one, for example (x=2, 4) returns 4.

It also sheds some light on object.child vs. object['child']. In the first example you need to use an actual key, while in the second one you pass a statement; in this case the statement evaluates to string, but it could be a variable name, or anything that would evaluate to something that can be used as a key (string or number).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.