0

When I first read the JSON format specification, I was under the impression that an object was a collection of key-value pairs (with the value being of any type) and an array was a collection of zero or more elements of the same type.

object (mixed values):

{
  "key": "value",
  "key2": 2
}

array (single type):

[1, 2, 3]
["one", "two", "three"]
[
  {"key": "value"},
  {"key2": 2}
]

I encountered some examples that included arrays with elements of mixed types. After testing that this is legitimate in Node.js, I'm left wondering: why have two different collection types that are so similar?

array (mixed types):

[1, "two", {"three": 4}]

The only major difference I can see is that arrays don't require a "key" for every "value". Are there any other significant differences?

EDIT:
Given the duplicate question of the original, and that an array is a dressed up object, I've modified the question slightly. (Apologies to the original answers.)

My best guess is that arrays are extremely common and adding a syntax and predefined class for them is mostly for convenience.

Possible duplicate/reference question: What is the difference between an array and an object?

Community
  • 1
  • 1
Ioan
  • 2,382
  • 18
  • 32
  • `arrays don't require a "key" for every "value"` Can you access an element without using its "index"? ;-) – thefourtheye May 20 '14 at 13:56
  • That's no different than specifying the property name for an object. I meant you don't need to define a "name" for every element like you do for every property in an object... – Ioan May 20 '14 at 13:59
  • what do you mean by *"why is it listed among the primitives?"* – Kevin B May 20 '14 at 14:21
  • @KevinB For the most part, all other subclassed objects are considered as such. Arrays seem to be a "given". For example, the JSON spec has it listed with the other primitives (boolean, numeric, string, object, null). – Ioan May 20 '14 at 14:31
  • What json specification are you looking at? i only know of two, and neither list arrays and objects as primitives. – Kevin B May 20 '14 at 15:31
  • @KevinB Granted, I didn't want to use the term "primitives" because it doesn't quite make sense, but I couldn't think of a better one. I'm basing my question on the "primitive" `values` listed here: http://json.org/ – Ioan May 20 '14 at 15:35
  • I see. It's listed that way because an arrays and objects can both be used as values the same way any of the primitives can be, allowing you to have nested arrays, nested objects, and a mixture of both. I don't see any reference that mention *"and an array was a collection of zero or more elements of the same type."* What exactly is your question at this point? – Kevin B May 20 '14 at 15:40
  • In javascript, an array is an object, however, that is not the case in json. in json, they are two different types of data structures, and a data structure can be used as a value. – Kevin B May 20 '14 at 15:43

2 Answers2

3

A "collection" can have many different attributes (in the meaning of "how it behaves"):

  • does it have a guaranteed order?
  • is it a key-value store where keys can be arbitrary?
  • is it homogeneous or can it be heterogenous?
  • does it grow as you put in items or does it have a fixed size?
  • etc. etc.

Any combination of these attributes gives you a different type of collection. These are variously known in different languages as lists, tuples, arrays, dictionaries, hashes, objects etc.. Each has a different tradeoff in terms of behaviour vs. strictness vs. complexity vs. resource usage etc.. Javascript chose to implement two types:

  1. lists, called "arrays" in Javascript:

    • has a guaranteed order
    • keys are numeric only
    • numeric keys are continuous (they're actually not, arrays are sparse, but when iterating over the collection it should be considered continuous)
    • can be heterogenous
  2. dictionaries, called "objects" in Javascript:

    • no guaranteed order
    • key-value associative collection
    • can be heterogenous

You choose between the two based on which attribute is important to you. The choice typically comes down to ordered vs. unordered and key-value association vs. no relevancy for keys. Note that Javascript's particular implementation of both is essentially all hash maps, because everything in Javascript is an object is a function is an object; but their usage differs nonetheless by the above points.

deceze
  • 510,633
  • 85
  • 743
  • 889
0
[] instanceof Object
> true

Arrays are objects with convenience methods [see this answer].

Besides their nature, of course you want an ordered structure you can iterate over [the array], and a key:value collection whose values you want to access via either dot or bracket notation [someObj.someKey or someObj[someKey]].

Community
  • 1
  • 1
moonwave99
  • 21,957
  • 3
  • 43
  • 64