JavaScript Red Book notes how to use objects to manipulate arrays

Source: Internet
Author: User

Object definition = = = Reference type, which describes the properties and methods of a class of objects
    • How to create a new object
New + constructor var person = new Object (); object literal means var person = {}; Note: The new object creation method, mostly uses the object literal creation method, this method can give the person to encapsulate the data the feeling. In fact, object literals are also the preferred way to pass large amounts of optional data to a function.
    • Accessing object property Methods
Access to object properties is using dot notation: Console.log (person.name); Access object properties can also use square brackets notation: console.log (serson["name"]); Note: The main advantage of using square brackets to access an object is that you can access the property through a variable. It is recommended to use dot notation to access objects.
    • Adding properties to an object
Methods for adding properties to an object: by assigning a value to an object, Shang has an object to add a new property. var person = {};p erson.lastname = "Chen";p erson.age = "56"; Lastname,age These are the properties of the object, and by assigning values to these properties, you understand that the Shang object adds a new property.
    • Ways to manipulate objects
Looping through the properties of an object: for...in statement for (a variable in object name in an object) {code to execute} var person = {list:{name: "Kevin", Age:56},{name: "page", age:26}}; var X;var txt = ""; for (x in person) {txt + = person[x];} console.log (TXT);
    • Ways to abolish objects
Sets the value of the object to null. You can force the object to be abolished and null to be an empty object. After each object is exhausted, it is abolished and the object is disposed. The abolition of this is only when the object has only one reference, if there are multiple references, be careful of the operation, because this will all the references are set to null;
    • How to create an array
New + constructor var sum = new Array (); omit new var sum = array (); array literal means var sum = [];
    • Array is assigned in creation
var sum = new Array (10);                       The lenght value is 10 for the array var sum = new Array ("10");         Create a string array var sum = array ("Red", "Blue", "green");                                    Create an array of 3 strings var sum = [];                                 Create an empty array var sum = [+];                            This is not allowed to create an array var sum = [,,,,,,,,]; This is not allowed to create arrays
    • Reading an array
Console.log (Sum[0]); Reads the first value in the array sum array, the number in square brackets is the value to access
    • detecting arrays
Array.isarray ()//The purpose of this method is to finally determine whether a value is in the array if (Sum.isarray (value)) {//operation of the logarithmic group ...}
    • Array conversion methods
 tostring ();                          ,         &NB Sp  //In the form of a string, return each value in the array valueof ();                          ,         &NB Sp      //Returns the array join (' | ');                          ,         &NB Sp  //The returned array is delimited by |, if no value is passed to this method, then comma-delimited  var color = [' red ', ' green ', ' black '];            console.log (color.tostring ());                  //red,green,blackconsole.log   color.valueof ();                    //[red,green,black]console.log (color.join (' | '));                    //red | Green | black 
    • Array default method, manipulating the value of the array
    1. Push ()//can receive arbitrary parameters and add parameters to the end of the array;
    2. Pop ()//Remove the last item from the end of the array and return the removed item;
    3. Reverse ()//Reorder method
    4. Sort ()//Sort method
          var values = [0,5,12,2,4];          function Bijiao (value1, VA Lue2) {      //comparison function                if (value1 < value2) {&nbs P                  return-1;           &NBSP;&NB Sp;   }else if (value1 > value2) {                    RE Turn 1;               }else{            &N Bsp       return 0;               }     &nbsp ;    }          Values.sort (Bijiao);                    //to pass the comparison function to the sorting method, the return value is: 0,2,4,5,12
    1. Slice (starting position, end position)//for deleting, inserting, replacing
var values = [0,5,12,2,4];           var num1 = Values.slice (0,2);                      Delete the first two items in the array, the position of the item to be deleted, and the number of items to delete console.log (NUM1); 12,2,4
    1. Concat ()//For connecting two or more arrays returns an array copy
var num = [2,3];            Console.log (Num.concat (4,5)); 2,3,4,5
    • An iterative approach to arrays
There are 5 iterative methods for each array. Each method receives 2 parameters: passed in functionThe value of the object of this function's scope. the incoming function receives 3 parameters: The value of the array ( Item), the position of the item in the array ( Index), the array object itself ( Array)。 Here are all the methods and functions:
    • Both the Every () and some () methods are used to query whether an item in an array satisfies a condition. The only difference between them is:
Every (function parameter) This function parameter must return TRUE for each item, and this method returns true; Some (function parameter) This function parameter is true for an item, and this method is true;
    • Filter (function parameter) This function parameter returns true for an item, and returns an array of this item as true
    • Foreech (function parameter) This method has no return value
    • Map (function parameter) returns a new array consisting of the results of each function call
var num = [1,2,4,5,8,6,2,1];var Mapresult = Num.map ( function( Item, Index, Array{//map parameter is a function that receives 3 parameters, namely Item,index,array return (item > 2); The condition of the running function});          Console.log (Mapresult); 4,5,8,6 returns a new array consisting of the result of calling this function;
    • Iterating through an array
JS iterates through an array in two ways:
    • For loop
var num = [5,15,6];                          for (var i = 0; i < num.length; i++) {Console.log (Num[i] + ","); 5,15,6}
    • For...in
var num = [5,15,6];                           for (var i in num) {Console.log (Num[i] + ","); 5,15,6}

JavaScript Red Book notes how to use objects to manipulate arrays

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.