Review of several test questions

Source: Internet
Author: User

First, the array to weight

Yesterday, when the written test, found that the data type JS method with the less familiar, or usually practice less, looked at the elevation did not remember the notes. The methods in the array type and the methods in the string are mixed.

The operations of the array are summarized as follows:

An array is simply an array of elements to be traversed, added, deleted, modified, inserted, sorted, searched (by the subscript to get the specified element), and the array is spliced and intercepted.

traversal: directly using for loop traversal;

add element: push (Value,value ...) (added at the end of the array), Unshift (Value,value ...) (added in front of the array)

(Both methods return the new array);

Delete element: pop () (delete the last element of the array), SHIFT (delete the first element of the array),

Splice (index,count) (delete count elements starting from the array element at the specified location)

(All three methods return the deleted element);

modifies the value of an element in an array and inserts a value at the position of the element : Splice (Index,1,value,value ... )

(If the second argument is 0, the directly inserted value does not delete the element at the specified position);

Sort: reverse () (array in reverse order, call method array is changed), sort ([function]) (can define a comparison rule function as parameter);

Find: indexOf (Value,[index]) (Find the subscript for the specified element, the second parameter represents the index where the start point is found), LastIndexOf () and IndexOf ()

The difference is that LastIndexOf is looking forward from the last element;

splicing: join ([value]) (value is a concatenation of rules, such as join (","), call method array immediately change),

Concat (Value,value ... ) (value can be either a string or an array, the original array will not change, the returned value is the new array);

Intercept: Slice (INDEX1,INDEX2).

The operation of the string is summarized as follows:

stitching, cropping, replacing (and also for deleting elements) of a string, getting the subscript of an element in a specified string, getting an element from a position in the string ,

Uppercase and lowercase conversions, removing the leading and trailing spaces in the string.

Stitching: cancat (Value,value ... ), the return is a copy, and the cancat usage in the array is similar.

cropping: slice (index1,[index2]), substring (index1,[index2]), substr (Index1,[index2]).

Replace with: replace (value1,value2), value1 can be a value to match, can be a string or a regular, value2 is a new element to replace.

gets the specified element subscript: indexOf (value,[subscript]) searches from backward to/lastindexof (value,[subscript]) from back to back, and the methods in the array are similar;

Gets the element that specifies the subscript: charAt (Index), Chatcodeat (index) (Gets the character encoding).

Casing Conversion Method: tolocaleuppercase ()/touppercase () tolocallowercase ()/tolowercase ();

Note: There are cancat, slice, and indexof/lastindexof in the string and array.

There are many ways to go heavy, the IndexOf () method has a browser-compatible problem, so the following is not a panacea method.

/* * Logarithmic group de-*/array.prototype.uniquel1 = function () {var i = this.length;var arr = [];while (i--) {if (Arr.indexof (This[i]) = = =-1) {Arr.push (this[i]);}} return Arr.sort ();} var arr2 = [1,2,3,2,4,7,9,4];console.log (Arr2.uniquel1 ());

Not to be continued ...

Review of several test questions

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.