0

I came across this code and was wondering what is it? Is it an array?

test = {a: [1,0,0], b:[0,1,0], c:[0,0,1]};  

How would I get the array for A for instance?

console.log(bases[a]);

^ Results in "Uncaught ReferenceError: a is not defined"

The output of console.log(test); is

Object {a: Array[3], b: Array[3], c: Array[3]}
user1950278
  • 1,685
  • 2
  • 10
  • 15
  • 1
    `bases.a` or `bases["a"]` assuming `test` is the same as `bases`. –  Jul 17 '13 at 21:13
  • It's an object. Use `test.a` or `test["a"]`. – freakish Jul 17 '13 at 21:14
  • 1
    possible duplicate of [What is JSON and why would I use it?](http://stackoverflow.com/questions/383692/what-is-json-and-why-would-i-use-it) – Aaron Kurtzhals Jul 17 '13 at 21:16
  • possible duplicate of [JavaScript Object Literals & Array Literals](http://stackoverflow.com/questions/500583/javascript-object-literals-array-literals) or [Meaning of '{ }' in javascript](http://stackoverflow.com/questions/6831920/meaning-of-in-javascript) – Bergi Jul 17 '13 at 21:18
  • 3
    @AaronKurtzhals What on earth does this have to do with JSON? – JJJ Jul 17 '13 at 21:18
  • Yes JSON. Javascript Object Notation. This syntax is JSON syntax. JSON is not only for sending serialized data over the internet, you can use it as well to declare an object like this in code. – GolezTrol Jul 17 '13 at 21:19
  • 3
    No, it is not. The object in the question is an actual object, not a notation. – JJJ Jul 17 '13 at 21:20
  • 2
    @GolezTrol: That makes no sense. A similar syntax can appear in many languages. So should we say this is XYZ language? Of course not. –  Jul 17 '13 at 21:21
  • @user1950278: On your second question see [JavaScript property access: dot notation vs. brackets?](http://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Member_Operators – Bergi Jul 17 '13 at 21:23
  • 3
    The *N* in JSON stands for *Notation*. JS objects are not JSON, period. Maybe Crockford should have called it JSION, "JavaScript-**inspired** Object Notation", perhaps we wouldn't be having this discussion then... And actually, the code in the question is a valid js object, but not valid JSON. @GolezTrol – bfavaretto Jul 17 '13 at 21:25
  • @Juhana The object in the question is code (plain text) containing an object notation. It will only be an actual object when the code is parsed and executed. Then there will be an object created in memory to which `test` holds a reference. That said, I must admit it is indeed not JSON. It's a JavaScript Object Literal, which is similar but not exactly the same a JSON (it is actually a superset of). – GolezTrol Jul 17 '13 at 21:26
  • 1
    I guess this is HTML: `var x = y z;` –  Jul 17 '13 at 21:27
  • 1
    @CrazyTrain Most browsers actually would be able to interpret that as HTML, but it's not well-formed since the `` tag wasn't closed. – Peter Olson Jul 17 '13 at 21:31
  • 1
    @PeterOlson: Well, when given the proper Content-Type header, browsers will interpret whatever they're given as HTML, whether it's well-formed or not. –  Jul 17 '13 at 21:34
  • Okay, I already admitted that JSON is not the same as object literals, and I'm *very* sorry I said so, and I will try very hard not to make that mistake again. But JSON is a fully compatible subset of the object literal syntax and anyone who thinks that that's just a convenient coincidence, think again. – GolezTrol Jul 17 '13 at 21:35
  • @GolezTrol: It's not fully compatible, though almost. –  Jul 17 '13 at 21:44
  • 4
    @GolezTrol [JSON is not quite a subset of JavaScript](http://timelessrepo.com/json-isnt-a-javascript-subset). – Peter Olson Jul 17 '13 at 21:59

5 Answers5

3

The output

Object {a: Array[3], b: Array[3], c: Array[3]}

shows that:

  • test -> is an Object
  • a, b and c -> are arrays

To access a/b or c, use:

console.log(test.a)
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
3

You are trying to pass in an undefined variable a.

Use bases.a or bases['a'].

instanceof me
  • 38,520
  • 3
  • 31
  • 40
2

The thing defined by the { } is an object.

a is a property of that object. You can get to a property by using a period . as the separator between the variable name containing the object (test) and the name of the proeprty (a). So to log the array, use console.log(test.a);.

The value of this property happens to be an array in this case, so you can get an item of the array a like this:

console.log(test.a[0]);
GolezTrol
  • 114,394
  • 18
  • 182
  • 210
2

It's an object with 3 arrays named a,b and c. test.a gives you the first array. In javascript {} is an object and [] is an array.

Rick Mangi
  • 3,761
  • 1
  • 14
  • 17
1

That is an object containing 3 arrays as values. To access the array for "a", you can use test.a (dot notation) or test["a"] (bracket notation).

To print to console:

console.log(test.a);

You can store almost anything inside of an object, including arrays and even other objects!

Pedootz
  • 52
  • 2