JavaScript array objects Common API functions summary (join, INSERT, delete, reverse, sort, etc.) _javascript tips

Source: Internet
Author: User
Tags array length javascript array

The examples in this article describe the common API functions for JavaScript array objects. Share to everyone for your reference, specific as follows:

1. Concat ()

Connect two or more arrays and return the results

var a = [1,2,3];
var B = A.concat (6,7);
Console.log (a); [1,2,3]
Console.log (b);//[1,2,3,6,7]

2. Join (STR)

Separates all elements of an array with STR, with the default comma separated

var a = [1,2,3]
var b = a.join (' | ');
Console.log (a); [1,2,3]
Console.log (b);//"1|2|3"

3. Pop ()

Deletes and returns the value of the last element of the array

var a = [1,2,3];
var B = A.pop ();
Console.log (a); [1,2]
Console.log (b);//3

4. Push ()

Adds one or more elements to the end of the array and returns the new array length

var a = [1,2,3];
var B = A.push (' 4 ', ' 5 ');
Console.log (a); [1,2,3,4,5]
Console.log (b);//5

5. Reverse ()

Reverses the order of elements in an array

var a = [1,2,3,4,5];
var B = A.reverse ();
Console.log (a); [5,4,3,2,1]
Console.log (b);//[5,4,3,2,1]

6. Shift ()

Deletes and returns the first element of the array

var a = [1,2,3];
var B = A.shift ();
Console.log (a); [2,3]
console.log (b);//1

7. Slice (start,end)

Returns the selected element from an existing array

var a = [1,2,3,4,5];
var B = A.slice (0,-1);
var c = A.slice (1,3);
var d = a.slice (1);
var e = A.slice (2,-2);
var f = a.slice (3,1);
Console.log (a); [1,2,3,4,5]
Console.log (b);//[1,2,3,4]
Console.log (c);//[2,3]
Console.log (d);//[2,3,4,5]
Console.log (e); [3]
console.log (f);//[]

8. Sort (func)

Sort the array by the specified parameters

Note: Func must return a value.

Negative value, the first argument is smaller than the second argument, and is in front.

0, equal.

Positive values, the first parameter is larger than the second parameter, and is in the back.

var a = [1,2,10,3];
var B = A.sort ();
Console.log (a); [1,10,2,3]
Console.log (b);//[1,10,2,3]
var num = function (x,y) {return
  x-y;
}
var c = a.sort (num);
Console.log (a); [1,2,3,10]
Console.log (c);//[1,2,3,10]

9. Splice (start,deletecount,val1,val2,)

Removes one or more elements from an array and inserts a new element at that location, returning the removed element

var a = [1,2,3,4,5];
var B = A.splice (2,2,7,8);
Console.log (a); [1,2,7,8,5]
Console.log (b);//[3,4]

Unshift (val1,val2,)

Inserts the specified element at the beginning of the array and returns a new length

var a = [1,2,3];
var B = A.unshift ( -2,-1);
Console.log (a); [ -2,-1,1,2,3]
Console.log (b);//5

ToString ()

Converts the elements of an array to strings, separated by commas, and connected

var a = [1,2,3,4,5];
var B = a.tostring ();
Console.log (a); [1,2,3,4,5]
Console.log (b);//"1,2,3,4,5"

More readers interested in JavaScript-related content can view the site topics: "javascript array Operation tips Summary", "JavaScript traversal algorithm and Skills summary", "JavaScript Mathematical calculation Usage Summary", " JavaScript data structure and algorithm skills summary, "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and techniques summary" and "JavaScript error and debugging skills Summary"

I hope this article will help you with JavaScript programming.

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.