Javascript Array Object vs Array Like Objects - Refinement

I tried to create an array-like object declared as an array object, and found that JSON.stringify did not correctly handle an array-like object when it was defined as an array object.

See below for clarity, β†’ jsFiddle

var simpleArray = []; //note that it is defined as Array Object alert(typeof simpleArray); // returns object -> Array Object simpleArray ['test1'] = 'test 1'; simpleArray ['test2'] = 'test 2'; alert(JSON.stringify(simpleArray)); //returns [] 

It worked fine and returned me {"test1":"test 1","test2":"test 2"} when I changed

var simpleArray = []; before var simpleArray = {}; .

Maybe someone shed some light or some link where I can find out more?

Edit:

Question: When the typeof simpleArray = [] and simpleArray = {} objects were returned, why couldn't JSON.stringify return {"test1":"test 1","test2":"test 2"} in both cases?

+6
source share
7 answers

You do not need an array. If you need a "associative array" in JavaScript, you really need a {} object.

You can distinguish them from instanceof Array :

 [] instanceof Array // true ({}) instanceof Array // false 

EDIT: he can handle it. It serializes all elements of the array. However, to be an element, there must be a digital key. In this case, they are not.

This is nothing special for JSON. This is consistent with toSource and the length property.

+2
source

The difference is the indices. When you use the [] array, indexes can only be positive integers.

So wrong:

 var array = [ ]; array['test1'] = 'test 1'; array['test2'] = 'test 2'; 

because test1 and test2 are not integers. To fix this, you need to use integer based indexes:

 var array = [ ]; array[0] = 'test 1'; array[1] = 'test 2'; 

or if you declare a javascript object, then the properties can be any lines:

 var array = { }; array['test1'] = 'test 1'; array['test2'] = 'test 2'; 

which is equivalent to:

 var array = { }; array.test1 = 'test 1'; array.test2 = 'test 2'; 
+4
source

"Maybe someone shed some light or some link where I can read more?"

When you are dealing with JSON data, you can refer to json.org to read about specification requirements.

In JSON , Array is a list of orders of values ​​separated by a character,.

enter image description here

So JSON.stringify() simply ignores everything that cannot be represented as a simple ordered list.

So, if you ...

 var simpleArray = []; simpleArray.foo = 'bar'; 

... you still provide an array, so it expects only numeric indices and ignores anything else.

Since JSON is language independent, JSON methods must decide which language structure is best for each JSON structure.

So JSON has the following structures ...

 {} // object [] // array 

It should be noted that although the appearance is very similar to JavaScript objects and arrays, they are not exactly the same.

Any JavaScript structures used to create JSON structures must match JSON structures. This is why non-numeric properties removed are excluded.

Until JavaScript objects to them, JSON will not tolerate them.

+2
source

When JSON.stringify encounters an array, it JSON.stringify same way as the for loop from 0 to simpleArray.length to find the values. For instance:

 var a = []; a[5] = "abc"; alert(JSON.stringify(a)); // alerts [null,null,null,null,null,"abc"] 

Therefore, setting properties on it will be completely invisible to JSON.

However, defining an object using {} makes JSON treat it as an object, and therefore it iterates over the properties of the object (excluding the inherited properties from the prototype chain). That way, it can find your properties test1 and test2 and successfully returns what you expect.

+1
source

The differences between Array instances and other objects are specified in ECMAScript Language Specification, Edition 5.1 , section 15.4.

You will find there that although you can use the bracket attribute access syntax with all object references -

 objRef[propertyName] 

- Array instances are special. Only the use of the parameter value, the string representation of which is a 32-bit unsigned integer value less than 2 32 -1, refers to the structure element of the encapsulated array, and write access affects the value of Array instance length .

Section 15.12 specifies the JSON object and its stringify method.

+1
source

In Javascript, everything is an object, so even arrays. This is why you get:

 >> var l = [1, 2]; >> typeof l "object" 

Arrays are stored in the same way as objects. Thus, arrays are just hash objects. The index you specified when accessing, for example,

 >> l[0] 

It is not interpreted as an offset, but is hashed and then a search is performed.

So, arrays are just objects (with some built-in array methods), and so you can think of them as objects and put a value under a specific key. But to calculate the length, only those keys are indexed by numbers.

 >> var l = [] >> l.length 0 >> l[5] = 1; >> l.length 6 >> l [undefined, undefined, undefined, undefined, undefined, 1] >> l.hello = "Hello" >> l.length 6 

Read Array - MDN for more information and Associative arrays are considered harmful , why you should not do this.

0
source

Your code is not semantically correct, but since most JavaScript engines are really good for a programmer, they make such mistakes.

Quick test case:

 var a = []; ab = "c"; console.log(JSON.stringify(a));//yields [], and not {"b":"c"} as you might expect 

This may be due to some rigor in JSON.stringify , which still treats a as Array . I would not be too worried about this. This is a situation that should not occur in your program, because it is a mistake. JSLint is a popular tool for detecting these and many other potential problems in your code.

-1
source

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


All Articles