Difference between var = {} and var = []?

Possible duplicate:
Objects versus arrays in Javascript for key / value pairs

I have a variable in JavaScript that I use as a hash. I can initialize it like this:

var selected = []; 

or

 var selected = {}; 

and he does the same. I use it, for example:

 selected["one"] = 1; if (selected["one"] == 1) console.log("one is selected"); // result: one is selected if (selected["two"] != 1) console.log("two is not selected"); // result: two is not selected selected["one"] = 0; if (selected["one"] != 1) console.log("one is no longer selected"); // result: one is no longer selected 

Is there any difference? Is the object an object and the other an array? If so, when should I run into problems. That is, what is the difference between them in their use and why did you choose one of them?

+6
source share
7 answers

[] is an array, {} is an object. An array is a type of object designed to assign only numeric keys.

Although you can theoretically use them interchangeably, see what happens when you JSON-ify them:

 var tmp = []; // or: var tmp = {} tmp.one = 1; JSON.stringify(tmp); // array: '[]' // object: '{"one":1}' 
+13
source

An array ( [] ) is a kind of object ( {} ). Key differences are:

  • Arrays have the magic property of length , which is equal to the highest set of numeric keys (plus one):

     var a = []; a[100] = 0; a.length; // is 101 var o = {}; o[100] = 0; o.length; // is undefined 
  • The Array toString method displays the values ​​of the numeric keys:

     var a = []; a[0] = 5; a[1] = 6; a[2] = 7; a.toString(); // "[5,6,7]" var o = {}; o[0] = 5; o[1] = 6; o[2] = 7; o.toString(); // "[object Object]" 
  • Arrays have many special functions for processing records:

     > Object.getOwnPropertyNames(Array.prototype) ["join", "toLocaleString", "sort", "some", "lastIndexOf", "splice", "map", "constructor", "every", "unshift", "shift", "indexOf", "pop", "forEach", "reverse", "reduce", "slice", "concat", "filter", "toString", "reduceRight", "push", "length"] 
+3
source

Yup, {} is an empty object, and [] is an empty array. Note that an array is a kind of object optimized for handling a list of values, but, like any Javascript object, it can take other attributes, such as "one" or "two" - this is how it can support methods like myArray.push(1) : push is an attribute of all arrays. In fact, you can even say myArray["push"] = someOtherFunction to myArray["push"] = someOtherFunction push without problems. Arrays, like all Javascript objects, support arbitrary key assignments.

Ultimately, however, this is due to off-screen performance. If you want to keep a consistent list of values, the array will handle it much better and offer some useful attributes like push , shift , length , etc. - plus, future developers will really find out what you are doing. If you just want to store key-value pairs, an object is the way to go because it is not bogged down at this extra weight.

In short, although they both support key-value pairs, they are completely different behind the scenes. Don't worry too much about it and use what works best for you.

+2
source

Using {} is an object that is the basis for every complex type in Javascript. The reason you can treat an array as an object is because in Javascript, an array is an object (but not vice versa).

Javascript square brackets are how you access the attributes of an object, just like a notation . in C, Java, Python, etc. It just happens that arrays have values ​​in attributes 0, 1, 2, etc. Arrays also have a whole bunch of other built-in attributes like length , shift , push , etc.

This is a simplification, but it is an easy way to understand what is happening.

0
source

Array is an Object, but Object is not (always) an array. An array has methods and properties that are not present in every object. Example

 [].length //0 {}.length //undefined typeof []['slice'] //function typeof {}['slice'] //undefined 

If you use an array as hashmap and try to use something already existing as a key, you will run into a problem

0
source

[] is for arrays, and {} is for simple objects. Note that array is an object with a number of methods and properties that allow you to model lists (e.g. push, slice, ...).

As stated in the MDN documentation, you should not use an array as an associative array , i.e. arrays should not be used as hash tables.

0
source

The first is an array , the second is a literal object :

 var a = {}; a.SomeProperty = 1; console.log(a.SomeProperty); var b = []; b['SomeProperty'] = 2; console.log(b['SomeProperty']); 

This will lead to output 1 and 2 in the console logs.

For hashing, use an array ( WRONG ), thanks for the comment.

-1
source

Source: https://habr.com/ru/post/923585/


All Articles