JSON array with properties

I have the following js array / object

var x = [1,2,3,4]; x.name = "myArray"; 

I am using json2.js and I am trying to serialize x in a string. and all i get is an array: [1,2,3,4]

it is right? since I can add some property to the array, why does json2 not work? What am I missing?

+6
source share
4 answers
  • First of all, json2.js ignores the properties in the array.
  • if not to ignore them, then it would be impossible to have an array in json format, which should be easily evaluated.

Suppose we exit with something like:

 [1,2,3,4]{name:'test',anotherProperty:'someValue'} 

if the above was valid javascript to create an array and paste two properties, then that would be great, and we could json-it. To do this would be equivalent to:

 array = [1,2,3,4] array.name = 'test'; array.anotherProperty = 'someValue'; 

But that is not the case, and the reason we cannot stay in json.

+2
source

What did you expect to see? :) I'm not sure what you are trying to do.

x is an array and everything you did when you did x.name , you just created the name property for the array object. The name property is not added to the array. You can see this in firebug:

enter image description here

An array object has a name property, but the value of this property is not added to the array itself.

What you want to do is something like this:

 var x = { array: [1, 2, 3, 4], name: "myArray" }; 

You now have an object named x that has two properties. The array property refers to the array, and the name property refers to the name. x.array will give you an array, and x.name will give you a name.

EDIT

This is the answer to your comments. Although it is true that arrays can be enriched with properties, think about what this means when you need to serialize this object. What exactly do you want to see when you serialize x ? If x already an array [1, 2, 3, 4] , how do you represent it in JSON? You can imagine it as:

 { 0: 1, 1: 2, 2: 3, 3: 4, name: "myArray" }; 

But you now have no array. In addition, the array object itself has a bunch of its own properties. How should the JSON serializer handle?

I think you are mixing an array as an object and an array as a literal. An array as a literal is simple [1, 2, 3, 4] . Now, internally, an array as an object supposedly has some property that indicates the actual value of the array. It also has other properties (e.g. length ) and methods acting on it (e.g. slice ).

The JSON serializer is associated with the value of the object. If x is an array, then the value to be serialized is the value of x , which is [1, 2, 3, 4] . The properties of x completely different.

If you want to serialize these additional properties, you will have to write your own serializer, which will iterate over the properties of the array and represent everything in the map / associative array. This is the only way; the caveat, as I mentioned earlier, is that you no longer have the actual array.

+2
source

The problem is that json2 still believes in an array, since x instanceof Array returns true. To fix this, you can create an "array" in a roundabout way:

 var x = { 0: 1, 1: 2, 2: 3, 3: 4, length: 4, name: 'myArray' }; 

The problem with this solution is that none of the array methods will be accessible to the object. Therefore, if you ever need to use pop, push, etc., you will need to convert it to a real array using Array.prototype.slice.call(x); or if you are byte hungry [].slice.call(x); .

0
source

Not sure why you will do this, but the for for loop will get everything

 var x = [1,2,3]; x.name = "wtf"; for(var y in x){ console.log(y + ":\t" + x[y]); } 

It produces:

 0: 1 1: 2 2: 3 name: wtf 

but json2 code won't see it that way, why don't you convert it to an object?

0
source

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


All Articles