JavaScript object and Array reference types

Source: Internet
Author: User

First, create an object instance

1. var person = new Object ();

2, var person = {};

Second, create an array

1, (1) var arr = new Array ();

(2) var arr = new Array (20);

(3) var arr = new Array ("name", "Sex", "age");

(4) var arr = Array ();

2, (1) var arr = [];

(2) var arr = ["Age", "name"];

Methods in the array:

var colors = ["Red", "Blue", "green"];

1, Array.isarray (colors);//detection array

2, var cStr = Colors.join ("| |"); red| | blue| | Green groups the array with the string passed into the join

3, var count = Colors.push ("orange");//Add an item at the end of the array and return the array length

4. var item = Colors.pop ();//Remove the item at the end of the array and return the removed item

5, var item = Colors.shift ();//Remove the item at the head of the array and return the removed item

5, var count= colors.unshift ("orange");//Add an item to the head of the array and return the array length

6, Colors.reverse ();//reverse array order

7, Color.sort ();//The array is sorted, you can call a function, when the return value is negative and 0, the wrong, if the positive is reversed.

Color.sort (Compare);

function Compare (value1,value2) {

if (value1>value2) {

return 1;

}else if (value1<value2) {

return-1;

}else{

return 0;

}

}

8, var color1 = Color.concat ("Red", "blue");//array addition, and return a new array

9, var color1 = Color.slice (1,3);//The entry in the intercept from the start position to the end position (excluding the end item), returns a new array, but does not affect the original array

10, Splice (2,1, "Red");

11, IndexOf (1, "red");//Backward Search

LastIndexOf (1, "red");//from Back to front

12. Traversal method

var cflag = color.every (function (item/* the current item */,index/* the current item's position */,arr/* the array itself */) {});//To iterate over each item of the array, true if all the items ' processing results return True

var cflag = color.some (function (Item,index,arr) {});//iterate over each item of an array, true if the processing result of an item returns true

var cflag = color.fliter (function (Item,index,arr) {});//iterate over each item of an array, return the item if the result of an item returns TRUE, and form a new array to return

var cflag = Color.map (function (Item,index,arr) {});//iterates over each item of the array, processes each item, returns the processing result of each item, and forms a new array to return

Color.foreach (function (Item,index,arr) {});//iterate over each item of an array

13, the reduction method

Reduce (function (prev/* */,cur/* The current item */,index/* the current item's position */,arr/* the array itself */) {//From left to right, iterating through all the items of the array, constructing a final value, returning the value

});

Reduceright (function (Prev,cur,index,arr) {//right-to-left, iterates through all the items of an array, constructs a final value, returns the value

});

JavaScript object and Array reference types

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.