Javascript objects and Arrays

Source: Internet
Author: User

In javascript, arrays and objects are two different data types, and they are also the two most important data types.

  Object

  1. Create an object

The simplest way to create an object is to add an object directly in javascript code. The direct object quantity is a list of attribute names and values separated by commas (,). Each attribute name contains a javascript identifier or a string, each attribute value can be a constant or any javascript expression.

        var empty = {};        var point = { x: 0, y: 0 };        var circle = { x: point, y: point.y + 1, radius: 2 };        var home = {            "name": "Homer Simpson",            "age": "34",            "married": true,            "occupation": "plant operator",            'email': "homer@example.com"        };

 

The new operator can create a specific class of objects. After new, a constructor is called to initialize object attributes.

        var a = new Array();        var b = new Date();        var r = new RegExp("haha","hehe");

 

  2. Object Attributes

The "." operator is usually used to store the attribute values of an object. The value on the left of the "." operator is the object to access its attributes. Generally, it only contains the referenced variable name of the object, but it can be a javascript expression of any result object. The value on the right of "." should be the attribute name, which must be an identifier rather than a string or expression.

Var book ={}; book. title = "java"; book. chapter1 = new Object (); book. chapter1.title = "c #"; book. chapter1.page = 11; book. chapter2 = {title: "c ++", page: 6}; // you can use alert to test

You can use the delete operator to delete attributes of an existing object.

        delete book.chapter2;

  3. As the object of the associated array

We have seen that the operator "." is used to obtain an object attribute, while the more common access attribute operator of arrays is [].

Book. chapter2; book ["chapter2"]; // The values of the two expressions are equal.

In C, C ++ and other strong speech types, the attribute stone of an object is fixed, and the names of these attributes must be predefined. Javascript is a loose language and does not adopt this rule. Therefore, you can create any number of attributes for an object in a program written in JavaScript. However, when the operator "." is used to retrieve the attributes of an object, the attribute names are marked with identifiers. In javascript programs, the identifiers must be input one by one. They are not a data type and therefore cannot be operated. However, when the [] representation of an array is used to access the attributes of an object, the attributes are represented by strings. Therefore, you can perform operations in the program.

        var a = { a1: 1, a2: 2, a3: 3, a4: 4 };        var b = 0;        for (i = 1; i <= 4; i++) {            b = b+a["a" + i];        }        alert(b);//10

 

4. Common object Attributes and Methods

No object has the constructor attribute, which references the constructor that initializes this object.

VaR d = new date (); // you can determine the object type if (typeof d = "object") & (D. constructor = Date) {alert ("is date");} else {alert ("not date ");}

 

  Array

Array array is an ordered set of values. Each value is called an element, and each element has a numeric position in the array compared to the subscript (index ). Javascript is a non-type language, so an array element can have any data type, and different elements of the same array can have different types.

Var empty = []; // empty array var a = [2, 3, 4, 5, 6]; var B = [1.1, true, "a"]; // The element can be var base = 1024 of different types; var table = [base, base + 1, base + 2, base = 3]; // The array element can make the expression var c = [[1, {x: 1, y: 2}], [2, {x: 3, y: 4}]; // The direct quantity of arrays can contain the direct quantity of objects or other direct quantities of Arrays

 

Another way to create an Array is to use an Array constructor.

Var a = new Array (); var a = new Array (1, 2, 3, "1", "2"); var a = new Array (10 ); // specify the length of the array

 

Read/write of array elements. The first subscript of the array is 0;

// Array you can use [] square brackets to store array var a = a [0]; a [1] = 3.14; I = 2; a [I] = 3; a [I + 1] = "hello"; a [a [I] = a [0];

As mentioned above, the [] operator can be used to access the named attributes of an object.

My ['a'] = 2; // because the array is a special object, you can define non-numeric Object Attributes on the array, and use, or [] syntax to access it.

The subscript range of an array is a 32-power integer between 0 and 2. If the subscript of an array is a negative number, floating point number, Boolean value, object, or other value, javascript will directly convert it into a string and use the generated string as the property name of the object.

Add new elements of the array

It seems that in C # and other languages, an array with fixed elements must be specified at the time of creation. In JavaScript, the array can have any number of elements, and the number of elements can be changed at any time.

// Add an element and assign a value to a [10] = 10;

 

Arrays in JavaScript are sparse. This means that the subscript of the array does not need to fall into a continuous number range. Only elements that are actually stored in the array can be allocated to the memory in JavaScript.

A [0] = 1; A [10000] = 10000; // The parser only allocates memory for elements whose subscript is 0 and 10000, instead of allocating memory for those 10000 elements between 0 and 9999

3. Delete array elements

The delete operator sets the element of an array to the undefined value, but the element itself continues to exist. To delete an element so that all the elements whose subscript position is higher than it are migrated down to a lower subscript position, an array method must be used.

VaR B = [1, 2, 3, 4, 5, 6, 7]; B. shift () // Delete the first element B. pop (); // Delete the last element B. splice (2, 3); // deletes the element of a range.

Array Length

All arrays are created either by the constructor array () or directly. They all have a special property length. Unlike Common Object properties, the Length attribute stone of the array is automatically updated.

        var a = new Array(); //a.length=0        a = new Array(10);   //a.length=10        a = new Array(1, 2, 3);   //a.length=3        a = [1, 2];     //a.length=2        a[5] = -1;  //a.length=6        a[49] = 0;  //a.length=50

Multi-dimensional array

Although JavaScript does not support real multi-dimensional arrays, it allows array elements to be arrays, which is very close to multi-dimensional arrays. To store an array as an element in the array, you only need to use the [] OPERATOR multiple times.

        var a = [[1, 2], [3, 4], [5, 6]];        alert(a[0][1]);//2

Array Method

// Join converts all elements of an array into a string var a = [1, 2, 3, 4, 5, 6]; // alert (. join (); // join converts all elements of an array into strings and outputs them as 1, 2, 3, 4, 5, 6 // alert (. join ('&'); // join converts all elements of an array into strings, you can also specify the separator output as 1 & 2 & 3 & 4 & 5 & 6 var B = "1, 2, 3, 5, 6, 7"; B. split (','); // the opposite of split and join is to split a string into arrays // alert (B [0]); // reverse reverses the order of an array element. reverse (); alert (a [0]); // concat can create and return an array, which contains the element var c = [1, 2, 3]; // concat can be. concat (4.5) // returns [, 5]

 

  

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.