Operation of JS Array

Source: Internet
Author: User
Tags javascript array

A very early article, I feel more clear.

I. Operation of array elements

1. Creation of arrays

vararrayObj = newArray();

var arrayObj = new Array(size); //创建一个数组并指定长度,注意不是上限,是长度

var arrayObj = [];

2. Access to elements of an array

vartestGetArrValue=arrayObj[1]; //获取数组的元素值

arrayObj[1]= "这是新值"//给数组元素赋予新的值

3. Adding array elements

arrayObj.push(item);// 将一个或多个新元素添加到数组结尾,并返回数组新长度

arrayObj.unshift(item); // 将一个或多个新元素添加到数组开始,数组中的元素自动后移,返回数组新长度 arrayObj.splice(insertPos,0,item); //将一个或多个新元素插入到数组的指定位置,插入位置的元素自动后移,返回[]空数组。

4. Deletion of array elements

arrayObj.pop(); //移除最后一个元素并返回该元素值

arrayObj.shift(); //移除最前一个元素并返回该元素值,数组中元素自动前移

arrayObj.splice(deletePos,deleteCount); //删除从指定位置deletePos开始的指定数量deleteCount的元素,数组形式返回所移除的元素 5. Interception and merging of arrays

arrayObj.slice(start, end); //以数组的形式返回数组的一部分,注意不包括 end 对应的元素,如果省略 end 将复制 start 之后的所有元素

arrayObj.concat(item);  //将多个数组(也可以是字符串,或者是数组和字符串的混合)连接为一个数组,返回连接好的新的数组

6, copy of the array

arrayObj.slice(0); //返回数组的拷贝数组,注意是一个新的数组,不是指向

arrayObj.concat(); //返回数组的拷贝数组,注意是一个新的数组,不是指向

7. Sorting of array elements

arrayObj.reverse(); //反转元素(最前的排到最后、最后的排到最前),返回新数组

arrayObj.sort();  //对数组元素排序,返回新数组

8. String of array elements

arrayObj.join(separator); //返回字符串,这个字符串将数组的每一个元素值连接在一起,中间用 separator 隔开。

toLocaleString, toString, valueOf: Can be seen as a special use of joins, not commonly used 9. Array of stringsString.Split ( separator ); //返回数组,这个数组将字符串以separator为分界,拆分成数组的元素。 usage of 10.splice

1) Delete: Splice (the number of items to delete in the position of the first item); Two parameter usage

2) Insert add: Splice (start position, 0, item to insert), insert before start position, number of items is variable,

3) Replace: Splice (starting position, number of items deleted, inserted item);

The splice () method returns an array that contains the deleted items. If not deleted, an empty array is returned.

Ii. 3 properties of an array object

1. Length Property

The Length property represents the size of the array, which is the number of elements in it. Because the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1. Unlike most other languages, the length property of a JavaScript array is variable, which requires special attention. When the length property is set larger, the state of the entire array does not change in fact, only the length property becomes larger, and when the length property is set to an earlier hour, the value of the element whose index is greater than or equal to length in the original array is lost. Here is an example that shows changing the length property:

var arr=[12,23,5,3,25,98,76,54,56,76];//定义了一个包含10个数字的数组 alert(arr.length); //显示数组的长度10
arr.length=12; //增大数组的长度
alert(arr.length); //显示数组的长度已经变为12
alert(arr[8]); //显示第9个元素的值,为56
arr.length=5; //将数组的长度减少到5,索引等于或超过5的元素被丢弃
alert(arr[8]); //显示第9个元素已经变为"undefined"
arr.length=10; //将数组长度恢复为10
alert(arr[8]); //虽然长度被恢复为10,但第9个元素却无法收回,显示"undefined"

By the above code we can clearly see the property of the length property. But the length object can be not only explicitly set, it may also be implicitly modified. You can use an undeclared variable in JavaScript, or you can use an undefined array element (an element that has an index that exceeds or equals length), at which point the value of the length property is set to the value of the index of the element being used plus 1. For example, the following code:

vararr=[12,23,5,3,25,98,76,54,56,76];

alert(arr.length);

arr[15]=34;

alert(arr.length);

The code also defines a 10-digit array, with the alert statement to see its length as 10. An element with an index of 15 is then assigned a value of 15, or arr[15]=34, and then the length of the array is output with the alert statement, resulting in 16. In any case, this is a surprising feature for developers who are accustomed to strongly typed programming. In fact, the initial length of an array created with the new Array () is 0, and it is the operation that does not define the element in it that changes the length of the array.

As you can see from the above introduction, the length property is so magical that it makes it easy to increase or decrease the capacity of the array. Therefore, an in-depth understanding of the length attribute can be used flexibly in the development process.

2. Prototype Properties

Returns a reference to the object type prototype. The prototype property is common to object.

Objectname.prototype

The ObjectName parameter is the name of the object.

Description: Provides a basic set of functions for a class of objects using the prototype property. The new instance of the object "inherits" the action given to the object's prototype.

For array objects, use the following example to illustrate the purpose of the prototype property.

Adds a method to the array object that returns the value of the largest element in the array. To do this, declare a function, add it to array.prototype, and use it.

functionarray_max(){

    var i,

    max = this[0];

    for (i = 1; i < this.length; i++){

        if (max < this[i]){

            max = this[i];

         }

     }

     return max;

}

Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);

var y = x.max();

该代码执行后,y 保存数组 x 中的最大值,或说 6。

3. Constructor properties

Represents a function that creates an object.

Object.constructor//object is the name of the object or function.

Description: The constructor property is a member of all objects that have prototype. They include all JScript intrinsic objects except the Global and Math objects. The constructor property holds a reference to a function that constructs a particular object instance.

For example:

x = newString("Hi");

if (x.constructor == String) // 进行处理(条件为真)


function MyFunc { 

// 函数体。

}

y = new MyFunc;

if (y.constructor == MyFunc) // 进行处理(条件为真)。

对于数组来说:
y = new Array();

Operation of JS Array

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.