Common methods for Array (array) objects and string (string) objects in JavaScript summary _javascript tips

Source: Internet
Author: User
Tags foreach string methods javascript array

The examples in this article summarize common methods for array (array) objects and string objects in JavaScript. Share to everyone for your reference, specific as follows:

Summary: The author often confuse the method of array and the method of string, write a log here, make a distinction

1. String Object

A string is one of five basic types in JavaScript.

(1) Creation of String objects

Example 1:

var str= "Hello World";

Or

var str=new String ("Hello World")

(2) CharAt () method

The CharAt () method returns the string at the specified position, for example, if we want to return the second character in the Str string, we can write charAt (1) because the subscript of the string also starts at 0, so we return the second character E;

Example 2:

var str= "Hello World"; Alert (Str.charat (1))
//Output E

(3) IndexOf (note O to capitalize)

IndexOf () method that returns the position of the first occurrence of a specified string value in a string.

Example 3:

var str= "Hello World"; Alert (Str.indexof (' e '))
//Output 1

The IndexOf () method can also have a second parameter that sets the position at which the string begins to be retrieved.

(4) Split () method

The split () method, which splits the string according to the rule. Like what:

Example 4:

var x= "86-029-19201920"; Alert (X.split (-))
//Output 86 029 19201920

The split () method can also have a second parameter, indicating the number of splits, if omitted, the default number of splits is not limited

(5) Substring () method

Substring () method, you can extract the string, the modified method has two parameters, the first parameter represents the starting position, the second parameter represents the termination position, if the second argument is omitted, the default extraction to the end of the string.

Example 5:

var x= "Hello World"; Alert (x.substring (0,4));
Output Hello

(6) substr () method

The substr () method can also be used to extract a string, which differs from the two arguments of the method, the first parameter represents the starting position, and the second parameter represents the number of the proposed string.

Example 6:

var x= "Hello World"; Alert (X.substr (0,5));
Same output Hello

2. Array objects (arrays)

(1) Creation of array objects

Example 1:

var arr=new Array (a);
var arr=[1,2,3];
var arr=new Array (1,2,3);
var arr=[];

There are several ways to create arrays, where Var arr=new array (1,2,3) is less common.

(2) Concat () method

Example 2:

var x=[1,2,3];
var y=x.concat (4,5);
alert (y);
Output Y is 1,2,3,4,5

The Concat () method is used to connect two or more arrays. This method returns a new array and does not change the original array.

(3) Join () method

Example 3:

var x=[1,2,3]; Alert (X.join ("-"));
Bit 1-2-3 of output

The join () method is used to put all elements in an array into a string. The elements are delimited by the specified delimiter.

(4) Reverse () method

Array is reversed and no new array is generated

Example 4:

var x=[1,2,3];
Alert (X.reverse ());
Bit 3,2,1 of output

(5) Slice () method

The slice () method returns the selected element from an existing array. The slice method also has 2 parameters, the first parameter represents the start position, and the second parameter represents the end position.

(6) Splice () method

The splice () method is a common method for inserting or deleting elements in an array, unlike the concat () and slice () methods, splice () modifies the array of calls.

Example 6:

var a=[1,2,3,4,5,6,7,8];
A.splice (4);//return [5,6,7,8],a array changed to [1,2,3,4]
a.splice (1,2);//return [2,3],a array changed to [1,4]
a.splice (1,1);//returned [4] , the array of a becomes [1]

Note: The first parameter of splice () specifies the starting position of the insertion, and the second parameter specifies the number of elements that should be inserted or deleted from the array

Splice () returns an array of deleted elements.

(7) push () and pop () method

The push () and Pop () methods allow arrays to be used as stacks, and push () means that one or more elements are added to the end of the array, and the Pop () method is the opposite.

(8) Shift () and Unshift () method

The shift () and Unshift () methods are very similar to the push () and Pop () methods, which are inserted and deleted at the head of the array rather than the tail.

(9) Sort () method

The sort () method arranges the elements in an array in a certain order.

Example 9:

The Var X=[1,2,13,113],alert (X.sort ())
//default collation is sorted alphabetically because the output 1,113,13,2

If you want the array to be sorted from small to large;

X.sort (function (a,b) {return a-b});

ES5 the New method

The array method in the ES5, the first of most methods receives a function, and the function is called once for each element of the array, and the parameter in the function is the array element of the first argument, the second parameter is the index of the element, and the third argument is the array itself

(a) foreach () method

Iterate through the array, calling the specified function for each array

Example 10:

var x=[1,2,3,4,5];
Alert (X.foreach function (x,i,a) {a[i]=x*x})
//back [1,4,9,16,25]

ES5 there are similar map,filter and other methods are not discussed.

3. Mutual use of Array objects and string object methods

We've learned the call and apply functions to implement string methods and the interoperability of array object methods.

For example, we want to use the array object's method in a String object, which can be implemented as follows:

Cases:

var x= "Hello World";
var y=array.prototype.slice.call (x);

Using the call method, the X string object is converted to a Y array object!!! Similarly, you can convert an array object to a string object.

It's not a drag! ~

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.

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.