Differences and relations between JS array, JS object, json array, json object and json string

Posted by johnnyblaze1980 on Wed, 09 Feb 2022 11:47:18 +0100

1. Concepts of arrays and objects

1 array

In the computer, the array storage mode of basically any language (plus basically because I don't understand all languages...), All data (or elements) of the same type are continuously stored in the address space, and they are also very similar in form, so it can also be described as this. An array is a group of continuous collections of the same type, and the subscript of the first element in the array is expressed as 0.

For example, arrays in js. In js, you can initialize the array as follows:

var num = new Array();
cars[0] = "one";
cars[1] = "two";
cars[2] = "three";

You can do the same

var arr = new Array("one","two","three");

You can do the same

var arr = [];
arr.push({"key1":"value1","key2":"value2"});
arr.push({"key3":"value3","key4":"value4"});
// When used
var value1 = arr[0].key1;
var value3 = arr[1].key3;

At this time, arr becomes an array of js objects. A pair of {} is equivalent to an array element, and there is a pair of key/value in it. Therefore, the elements of the array can be basic types or complex types, such as json strings

var arr = new Array(
    '{"name":"Runoob", "url":"www.runoob.com"}',
    '{"name":"Google", "url":"www.google.com"}',
    '{"name":"Taobao", "url":"www.taobao.com"}');

When the js code requests the server, because it sends a string, it needs to change the data into a string. If it is a json object, the browser will send it in the form of & name = value $Name2 = val2

$.ajax({
             type: "POST",
             dataType:"json",
             url: "a url",
             data: {"name1":"val1","name2":"val2"},
             success: function(data){}
});

The data returned by the request is object, and the object in js is very similar to json object. You can use data in this way Key to get the value;

The json array is more beautiful, because no matter how many brackets there are, the innermost elements are data in the basic json format composed of key/value, and there will be no special things.

{
"id":10086,
"task":"send",
"sites":[
    {"name":"Runoob", "url":"www.runoob.com"}, 
    {"name":"Google", "url":"www.google.com"},
    {"name":"Taobao", "url":"www.taobao.com"}
],
 "location":[
    {"name":"beijing","in":["north","east"]},
    {"name":"chongqing","in":["south","west"]}
]
"}

2 object

Everything in js can be an object, just as everything in c + + is an object. Only this object has more methods and properties. Of course, there are methods and properties in c + +

You can add methods and properties to js objects,

var name = "one name"; // js object
var name_len = name.length; // Properties of js object
var name_cap = name.toUpperCase(); //Method of js object

You can also use the constructor to add methods to your own objects like the built-in function above:

			function person(name,age)
			{
				this.name = name;
				this.age = age;
				this.len = function(){
					return (this.name.length + this.age.length);
				}

			}
			p = new person("liu","122");
			alert(p.len());// 6

Conceptually, a js object can be a json object, which is not necessarily a js object.

js objects can be any type and form of data, just like the instantiated objects of custom classes in c + + or java, and json objects are just one of them. Next, add two attributes to obj, where type2 is a json object

// Create js object
var  obj = new Object();
// Adding properties to an object
obj.type1 = "value";
obj.type2 = {"name":"value"};
alert(obj.type1);
alert(obj.type2.name);

As mentioned above, the json object is in the form of key/value

var json = {"name1":"value1", "name2":"value2"};

The json string is just to change the json object into a string with quotation marks. In this case, it is text, so you can't use obj Name1 to get the value.

var json_text = '{"name1":"value1", "name2":"value2"}';

However, you can use parse() method to convert json string into js object or json object to obj Name1 gets the value in this way

var json_text = '{"name1":"value1", "name2":"value2"}';
var obj = JSON.parse(json_text);
alert(obj.name1);

 

Topics: Javascript JSON