Data structure and algorithm JavaScript description notes--Array Chapter

Source: Internet
Author: User
Tags array sort shallow copy

definition of an array :

an array in JavaScript is a special object that represents an index of an offset that is a property of that object, which is likely to be an integer. However, these numeric indexes are internally converted to string types because The property name in the JavaScript object must be a string. is classified internally as an array. Because the Array is treated as an object in JavaScript, it has many properties and methods that can be used during programming.

Using arrays:

1. Create an array

① uses the [] operator,var arr=[], which is the most efficient method.

② creates an array with the constructor of the array,var myarr=new arrery ()

2. Read and write arrays

3. Generates an array from a string that invokes the split () method of the string object .

4. Integral operation of an array: (note here, once the question appears)

shallow copy: b The array at this time b a a When the change occurs, the array b

var a=[];  for (var i=0;i< 5;i++) {    a[i]=i;} var b=A;console.log (a[1]); Console.log (b[1]); // Assignment Reference top 1 a[1]=999; Console.log (a[1]); Console.log (b[1]); // 999 after assignment reference

deep Copy: You can encapsulate a copy () method.

Access array:

1. Find element:indexOf () lastIndexOf ()

2. String representation of an array: converting an array to a string:join () toString ()

Speaking of ToString () Here, I remember the implicit invocation of ToString () and valueof () . Here is very interesting, also slightly pits, not rigorous, when the need for a formula, will be implicitly called valueof (). ToString () is called when data or results need to be displayed . Interested students can Google a bit.

3. Create a new array from an existing array:

The concat () method merges multiple existing arrays, creating a new array

The splice () method intercepts a subset of an array to create a new array

Variable functions:

1. To add elements to an array:

The push () method adds the element to the end and unshift () adds the element to the beginning

2. To remove an element from an array:

The pop () method can delete the element at the end of the array

Shift () method to delete the first element of an array

3. To add and remove elements from an array's middle position:splice ()

4. Array Sort: Positive order ( dictionary order ):sort (), reverse ; reverse ()

Iterators:

Each element of an array uses a function that can return a value, a set of values, or a new array

    • An iterative method that does not generate a new array ; ForEach () every () some () reduce ()
    • An iterative method for generating a new array ; : map () and the filter () .

Two-dimensional and multidimensional arrays:

1. Creating a two-dimensional array

JavaScript supports only one-dimensional arrays, but you can create multidimensional arrays by saving array elements in an array. A two-dimensional array resembles a data table consisting of rows and columns. Create a two-dimensional array in JavaScript, create an array first, and then let each element of the array be an array.

This expands the array object by adding a new method that sets the number of rows, columns, and initial values of the array. Here is the definition of this method (reference javascript:the good Parts(O'Reilly) A piece of code ):

function (NumRows, numcols, initial) {    var arr = [];      for (var i = 0; i < numrows; + +i)        {var columns = []        ;  for (var j = 0; J < Numcols; + +j)            {= initial        ;        } = columns;    }

2. Processing mode i: Using embedded for Loop

Object array:

Arrays contain objects In addition to basic data type elements (numbers, strings), and the methods and properties of arrays still apply to objects. In an object, you can use an array to store complex data.

Note: This is the process note that I read the outline content of the data structure and algorithm JavaScript description, the students who need to study deeply, but also the original book. Beginners can use this as an outline for their studies. As for the good people, ignore this article, of course, can make suggestions, I will revise.

This series of articles is continuously updated.

Data structure and algorithm JavaScript description notes--Array Chapter

Related Article

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.