JavaScript Advanced (not finished)

Source: Internet
Author: User
Tags array length

Collection array
    1. 2 Ways to create arrays

      var fruits = [];
      var friuits = new Array ();

    2. Traverse

      Fruits.foreach (function (item, index, array) {
      Console.log (item, index);
      });
      Apple 0
      Banana 1

    3. Basic operations

action code return value
add element to end of array fruits.push (' Orange ') array length
add element To the head of the array fruits.unshift (' Strawberry ') array length
delete header element fruits.shift (); Header Element
delete trailing elements fruits.pop (); trailing element
to find out the index of an element in an array fruits.indexof (' Banana '); Subscript
delete an element by index fruits.splice (index, 1); deleted elements
copy array var shallowcopy = Fruits.slice (0,length); returns a new array of elements within the specified range
generate array array.from () Array.from () method creates a new array instance from a similar array or an iterator object.
    1. Example of deleting elements based on an index

      /* Splice (Start:number, Deletecount:number, ... items:t[]): t[]; */

      var fruits = ["Apple", "B", "C", "D"];
      Console.log ("Array is:");
      Fruits.foreach (function (item, index, array) {
      Console.log (item, index);
      });
      var index = fruits.indexof ("B");
      Fruits.splice (index,1);
      Console.log ("Array is:");
      Fruits.foreach (function (item, index, array) {
      Console.log (item, index);
      });

    2. Array.from () Use example

      var fruits = ["Apple", "B", "C", "D"];
      var f = array.from (fruits);
      F.foreach (function (item, index, array) {
      Console.log (item, index);
      });
      Apple 0
      B 1
      C 2
      D 3

      var f = array.from ("Hello");
      F.foreach (function (item, index, array) {
      Console.log (item, index);
      });
      H
      E
      L
      L
      O

      Array.from () can also be used for Set,map

Setmap

JavaScript Advanced (not finished)

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.