"Front End Learning Notes" JavaScript small case Collection

Source: Internet
Author: User

Get a random number of 0-9:

Math.Round (Math.random () *9);

To remove duplicate elements from an array:

var arr=[1,3,5,4,3,3,1,4]function editarr (arr) {var result=[];for (var i=0; i<arr.length; i++) {    if ( Result.indexof (arr[i]) = =-1) {      Result.push (arr[i]);}    } Console.log (result);}      Editarr (arr);

The + function parsequery is used to parse URL query parameters .
-The syntax is as follows:
-var obj = parsequery (query)
-query is the parsed query parameter, and the function returns the parsed object.
-Use the following example:
-var Jerry = Parsequery ("name=jerry&age=1");
-jerry; Return value: {name: "Jerry", Age: "1"}
-var Tom = Parsequery ("Name= Tom &age=12&gender&");
-tom; Return value: {name: "Tom", Age: "" ", Gender:"}
-Please write out the implementation code of the function Parsequery.

Method One:

function Parsequery (query) {var arr = query.replace (/\s+/g, "). Split (' & '); var obj = {};for (var i = 0; i < arr.length ; i++) {if (Arr[i]) {var newArr = arr[i].split (' = '), if (!newarr[1]) {newarr[1] = ';} Obj[newarr[0]] = newarr[1];}} return obj;}

Method Two:

function Parsequery (query) {var reg =/([^=&\s]+) [=\s]* ([^=&\s]*)/g;var obj = {};while (reg.exec (query)) {        OBJ[REGEXP.$1] = regexp.$2;} return obj;}

"Front End Learning Notes" JavaScript small case Collection

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.