Analysis of JavaScript array object array object with class

Array (Array objects)

Array is a set of data.

No array of this data type in JavaScript. When an array object is created.

  • Key (subscript): a flag for distinguishing different values ​​of the array is the key, the key is the initial 0.
    • As the subject of digital key, which is called an array indexed array.
    • Subject to the character string as key, this array is called an associative array
    • Note: In JavaScript only indexed array, y no associative array must be continuous from 0 indexed array!
  • Value: each key is the value corresponding to the data.
  • Key-value pair: the key value is the key to +

Operation array

Create an array method

  • new Array();
    • Variables var = new Array (); to create an empty array
    • Variables var = new Array (value, value ...); an array of a specified element
    • Variables var = new Array (integer); an array of a specified length
  • Using object literals
    • var variable = []; Create an empty array
    • var variable = [value, value ...]; an array of a specified element
    • var variable = [integer]; create an array element specified integer, not ~ length!

Add and modify array element

  • Array variable [array variable .length] = value;

    a [a.length] = 1; // Add

  • Array variable [specified index] = new value;

    a [0] = 1; Review

  • Array variable [exceed the current value of the array length] = value;

    = A [l, 2,3];

    A [. 5] =. 5;

    Output: [1, 2, 3, empty × 2, 5]

Delete array element

  • delete array variable [specified index];

    deldet a[0];

  • Note: in this way can remove only the array element values, the key can not be deleted, then the deletion, the length of the array is left unchanged. If you want to completely remove elements need to splice this method.

The use of array elements

  • Array variable [specified index];

Through the array elements

  • The first

    var x = [1,2,3];
    for (var i=0;i<x.length;i++){
        console.log(x[i]);
    }
  • The second

    var x = [1,2,3];
    for (var i of x){
        console.log(i);
    }

    The following are output

    1
    2
    3

Multidimensional Arrays

Popular interpreted as follows

  • One-dimensional array:

    If all values ​​are not an element of the array, then the array is a one-dimensional array.

  • Two-dimensional array:

    If a one-dimensional array of elements contained in a one-dimensional array, the array is two-dimensional array

  • Three-dimensional array:

    If the elements of a one-dimensional array contains a two-dimensional array, the array is a three-dimensional array

  • Multidimensional Arrays:

    If the dimension of an array of more than three, collectively known as multidimensional arrays

  • Multidimensional array access operations:

    Array variable [Key] [key] ...

An array of related functions

concat()

  • Connected array elements
  • format:

    = New variable array variable .concat (value, value ...)

    var a.concat B = (A,. 1, "X");

  • Parameter value, the value may be normal or may be an array type, can be connected.
  • Is added to the array behind every array, the value is added to the normal value Normal

join()

  • Connect using character array elements into a string, the default is,
  • format:

    = New variable array variable .join (developing string)

    var a.join B = ( ""); // ""null character string is not connected is equal to

pop()

  • Pops an element at the end of an array (directly change the original array)
  • format:

    = Outcome variable array variable .pop ();

    var a.pop B = ();

  • Note: This method returns an array of the original value of the direct operation of the pop-up element.

push()

  • Add an element at the end of an array (directly change the original array)
  • format:

    .Push array variable outcome variable = (value, value ..)

    var B = a.push ( ". 1", "2");

  • Note: The original method of operating an array of arrays direct the return value is added after the length.

shift()

  • In the beginning of the array removes an element (directly change the original array)
  • format:

    = Outcome variable array variable .shift ();

    var a.shift B = ();

unshift()

  • In the beginning of the array to add elements (directly change the original array)
  • format:

    = Outcome variable array variable .unshift (value, value ...)

    var B = a.unshift ( ". 1", "2");

    reverse()

  • Array reverse method
  • format:

    = Outcome variable array variable .reverse ();

    var a.reverse B = ();

sort()

  • Array sort method
  • String sorting using ascii sorting operation.
    • format:

      = An array variable outcome variable .sort ();

      var a.sort B = ();

  • Digital ordering, using digital ordering size
    • format:

      = Outcome variable array variable .sort (callback);

      var a.sort B = (C);

    • The callback function format requirements:
      • It must pass two parameters.
      • Returns must be positive, negative or zero;

slice()

  • Cutting the array, and wherein the return section
  • format:

    = An array variable .slice outcome variable (start subscript, superscript end);

    var a.slice B = (l, 3);

  • Note: The element contains the starting position, but does not contain the elements of end position
  • Wherein the parameter is a location parameter, a positive number indicates the number of the front rearwardly, forwardly from the rear negative number.

splice()

  • Universal method of operating an array (directly change the original array)
  • Add the element at the specified location

    = Outcome variable array variable .splice (developing position, 0, new elements ..)

    var a.splice B = (1,0, "the ABC");

  • Removing elements in the development position

    = Outcome variable array variable .splice (developing position, the number of deleted);

    var a.splice B = (1,2);

  • Replace elements in the development position

    = Outcome variable array variable .splice (developing position, the number of deleted, additional elements)

    var a.splice B = (1,2, "the ABC", "the DEF");

Class array of objects

There is a similar property with an array of special objects in JavaScript, which we call class array of objects, the most common is the argumengs object.

definition

  • Elements can be accessed through the index and has length property;
  • Other methods no array, e.g. push, forEach, indexOf like.

For example

Class Array

    var arrLike = {
        0: 'Java',
        1: 'Python',
        2: 'PHP',
        length: 3
    }

With an array of models

var arr = ["Java", "Python", "PHP"];

Contrast array

  • Array-object access, assignment, and acquisition operations on the length of the array is the same
    • access

      console.log(arr[0]); // Java

      console.log(arrLike[0]); // Java

    • Assignment

      arr[0] = 'JavaScript';

      arrLike[0] = 'JavaScript';

    • Get the length of the object

      console.log(arr.length); // 3

      console.log(arrLike.length); // 3

  • Array object and class distinction array is an array of objects can not directly use the class method array
    • Class objects being given array using array

      arrLike.push("C++"); // Uncaught TypeError: arrLike.push is not a function

Change

Sometimes we need to have an array of class characteristics of the array, this time on the need to convert, either directly or indirectly convert convert.

indirect

Function.apply method or by Function.call

  • Function.call
Array.prototype.push.call(arrLike, 'C++');
console.log(arrLike);
 // { '0': 'Java', '1': 'Python', '2': 'PHP', '3': 'C++', length: 4 }
  • Function.apply
Array.prototype.push.apply(arrLike, ['C++']);
console.log(arrLike);
 // { '0': 'Java', '1': 'Python', '2': 'PHP', '3': 'C++', length: 4 }

direct

  • Array.prototype.slice the array into an array class

    Array.prototype.slice.call(arrLike,0);

    Array.prototype.slice.apply(arrLike,0);

  • Array.prototype.splice array to array-convert

    Array.prototype.splice.call(arrLike,0);

    Array.prototype.splice.apply(arrLike,0);


Guess you like

Origin www.cnblogs.com/aduner/p/12229685.html