Encapsulation of common array-string operations with simple grooming

Source: Internet
Author: User
Tags new set

. Move an array of all the values in arr with an item equal to the element. Do not directly modify the array arr, the result returns a new array
Console.log (Move ([2,3,4,5,6,11,1,1,1,1,1,1,4,5,6],1))
function Move (Arr,item) {
Return Arr.filter (function (arr) {
Return ARR!== Item
})
}
Finding the largest value in a number class array
    Console.log (Maxarr ([1,2,3,4,5]))
function Maxarr (arr) {
Return Math.max.apply (Null,arr)
}
Finding the smallest value in a numeric class array
    Console.log (Maxarr ([1,2,3,4,5]))
function Minarr (arr) {
Return Math.max.apply (Null,arr)
}
Gets the random number of an array
        Console.log (Randomone ([1,2,3,4,5,6]))
function Randomone (arr) {
Return Arr[math.floor (Math.random () *arr.length)];
}
/*
1: Capitalize first letter
2: First Letter lowercase
3: Size conversion requires uniform distribution of both ends
4: All Caps
5: All Caps
*/
function ChangeCase (str, type) {
Switch (type) {
Case 1:return Str.replace (/^ (\w) (\w+)/,function (v,v1,v2,v3) {
Console.log (v)//matched array
Console.log (v1)//is the first capturing group
Console.log (v2)//is a second capturing group
Console.log (v3)//First matched index
return v1.touppercase () + v2.tolowercase ();
});
Case 2:return Str.replace (/^ (\w) (\w+)/,function (v,v1,v2,v3) {
Console.log (v)//matched array
Console.log (v1)//is the first capturing group
Console.log (v2)//is a second capturing group
Console.log (v3)//First matched index
return v1.tolowercase () + v2.touppercase ();
});
Case 3:return Str.replace (/^ ([a-z]+) ([a-z]+]/,function (v,v1,v2,v3) {
Console.log (v)//matched array
Console.log (v1)//is the first capturing group
Console.log (v2)//is a second capturing group
Console.log (v3)//First matched index
return v1.touppercase () + v2.tolowercase ();
});
Case 4:return str.touppercase ();
Case 5:return str.tolowercase ();
}
}
Console.log (ChangeCase ("QWEQWEWEWREWDSFGDJJ", 3))
String loop replication
function Repeatstr (str,count) {
var text= "";
for (Var i=0;i<count;i++) {
Text +=str
}
return text;
}
Detecting strings
function Checktype (str,type) {
Switch (type) {
Case "Email":
Return/^[\w-]+ (\.[ \w-]+) *@[\w-]+ (\.[ \w-]+) +$/.test (str);
Case "Phone":
return/^1[3|4|5|7|8][0-9]{9}$/.test (str);
Case "Tel":
Return/^ (0\d{2,3}-\d{7,8}) (-\d{1,4})? $/.test (str);
Case "Numer":
return/[^0-9]$/.test (str);
Case "Numerall":
return/[0-9]$/.test (str);
Case "中文版":
return/^[a-za-z]+$/.test (str);
Case "Chinese":
return/^[\u4e00-\u9fa5]+$/.test (str);
Case "Lower":
return/^[a-z]+$/.test (str);
Case "Upper":
return/^[a-z]+$/.test (str);

}
}

Detect password strength
function Checkpwd (str) {
var nowlv=0;
if (str.length<6) {
return NOWLV;
}
if (/[0-9]/.test (str)) {
nowlv++
}
if (/[a-z]/.test (str)) {
nowlv++
}
if (/[a-z]/.test (str)) {
nowlv++
}
if (/[\.| -|_]/.test (str)) {
nowlv++
}
Return NOWLV
}
Math.random () outputs 0 to 1 (including 0, excluding 1) of random numbers.

ToString (16) converts the random number to a 16-binary string.
SUBSTRING (2) intercepts the string because the random number is greater than or equal to 0 less than 1, the first two bits are "0.", and substring (2) is intercepted from the third bit to the last.

function Randomnumber (count) {
Return Math.random (). ToString (count). substring (5);
}
Find string occurrences of repeated string returns
function Countstr (str,strsplit) {
Return Str.split (strsplit). length-1;

}
ES6 simple method of weight-off
function Removerepeatarray (arr) {
Return Array.from (new Set (arr))
}
    Returns the number of occurrences of an array (string) element
function Getelecount (Obj,ele) {
var num=0;
for (Var i=0,len=obj.length;i<len;i++) {
if (Obj[i] ===ele) {
num++
}
}
}
Returns the element and number of occurrences of an array
Rank is the number of rows in the final row, Ranktype is 1 sequence display
function GetCount (arr,rank,ranktype) {
var obj={},k,arr1=[];
Record the number of occurrences of each element
for (Var i=0,len=arr.length;i<len;i++) {
K=arr[i];
Console.log (Obj[k])
if (Obj[k]) {
obj[k]++;
}else {
Obj[k]=1;
}
}
Save result el-element, count-occurrences
for (var o in obj) {
Arr1.push ({El:o,count:obj[o]});
}
Sort (Descending)
Arr1.sort (function (n1,n2) {
Return N2.count-n1.count
})
If the Ranktype is 1, the order is reversed and the data
if (ranktype===1) {
Arr1=arr1.reverse ();
}
var Rank1=rank | | Arr1.length;
Return Arr1.slice (0,RANK1)

}
Detects if the object has a class name
/\s+/matches any whitespace character, including \n,\r,\f,\t,\v, etc. (newline, carriage return, Space, tab, etc.)


var ww=document.getelementsbyclassname (' QQ ')
function Hasclass (OBJ,CALSSSTR) {
Convert a string to an array
var arr=obj.classname.split (/\s+/);
Console.log (arr)
Return (Arr.indexof (CALSSSTR) ==-1)? false:true
}
Randomly generated colors
function Randomcolor () {

Return "Rgba (" + randomnumber (255) + ', ' + randomnumber (255) + "," + randomnumber (255);
}

Encapsulation of common array-string operations with simple grooming

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.