JS array processing

Source: Internet
Author: User
Tags array to string

A. Define an array:

Method 1

var mycars=new Array (); mycars[0]= "Saab"; Mycars[1]= "Volvo"; mycars[2]= "BMW";

Method 2:

var mycars=new Array ("Saab", "Volvo", "BMW");

Method 3.

var mycars=["Saab", "Volvo", "BMW"];

Two. Array modification using for loop traversal

① in the array to specify the worthy location

var x=mycars.length        //Array length Y=mycars.indexof ("Volvo")  //a worthy location

② merging Arrays concat ()

var hege = ["Cecilie", "Lone"];var stale = ["Emil", "Tobias", "Linus"];var children = hege.concat (stale);

③ Array to string ToString () or join () "Join can arbitrarily set the interval character ToString no"

var fruits = ["Banana", "Orange", "Apple", "Mango"];var t =fruits.join ("--");

Results: Banana--orange--apple--mango

④:slice ()//The original array did not change

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];var citrus = fruits.slice (1,3),//1 is the length of index 3

Results: Orange,lemon

⑤:splice ()//change the original array directly

var fruits = ["Banana", "Orange", "Apple", "Mango"];fruits.splice (2,0, "Lemon", "Kiwi"), change the original array directly

Results: Banana,orange,lemon,kiwi,apple,mango

JS array processing

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.