JavaScript notes--arrays

Source: Internet
Author: User

Gets the array element index:

IndexOf returns the first index found: can be taken with a start index

LastIndexOf returns the last index found: Available with start index

var animals = new Array ("Dog", "cat" , "seal", "Elephant", "Walrus", "Lion", "cat")

Console.log (Animals.indexof ("cat")); Prints 1
Console.log (Animals.lastindexof ("cat"));//prints 6

Console.log (Animals.indexof ("Cat", 2)); Prints 6
Console.log (Animals.lastindexof ("Cat", 4)); Prints 1

Merging multidimensional arrays into one-dimensional arrays: Concat with Apply method

var fruitarray = [];
Fruitarray[0] = [' Strawberry ', ' orange '];
FRUITARRAY[1] = [' lime ', ' peach ', ' banana '];
FRUITARRAY[2] = [' tangerine ', ' apricot '];
FRUITARRAY[3] = [' Raspberry ', ' kiwi '];

var newarray = Fruitarray.concat.apply ([], Fruitarray);
Console.log (Newarray[5]);

To delete or replace an array element: IndexOf Splice

Splice () method takes three parameters; The first parameter is Required,as

It ' s index where the splicing is to take place; The second,optional parameter is

The number of elements to remove; The third parameter, also optional, is a set of

The replacement elements.

Animals.splice (Animals.indexof ("Walrus"), 1);
Animals.splice (Animals.lastindexof ("Cat"), 1, "monkey");
Console.log (Animals.tostring ());

JavaScript notes--arrays

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.