Foreword
Some methods exist for arrays when writing JavaScript code, the page may be involved in a lot, and every time I go to write a bunch of code. Long-term effect of the particular code 繁多
, it's time to wave the package, did not talk much to start writing 优美
code
Code has been uploaded github, need to welcome Star ( HTTPS: //github.com/Xieguoiang ... ).
On the array of package methods
1. Array去重
`上文提到的Set的封装`
//ES6新增的Set数据结构,类似于数组,但是里面的元素都是唯一的 ,其构造函数可以接受一个数组作为参数
//ES6中Array新增了一个静态方法from,可以把类似数组的对象转换为数组
//方法二 new
function removeRepeatArray(arr){
return Array.from(new Set(arr))
}
2. Array顺序打乱
function upsetArr(arr){
return arr.sort(function(){ return Math.random() - 0.5});
}
3. The array of most 大
value most 小
value
// this piece of packaging, primarily for digital array of type
function maxArr(arr){
return Math.max.apply(null,arr);
}
function minArr(arr){
return Math.min.apply(null,arr);
}
4 array 求和
,平均值
this piece of packaging, primarily commodity demand for digital-type array total price of the total demand for a very popular
summation
function sumArr(arr){
var sumText=0;
for(var i=0,len=arr.length;i<len;i++){
sumText+=arr[i];
}
return sumText
}
The average value, the decimal point may be a bit much, do not make your treatment may need to 保留多少位
deal with their own look at it ~ ~
function covArr(arr){
var sumText=sumArr(arr);
var covText=sumText/length;
return covText
}
5 array from the 随机
acquired element
//类似抽奖了什么的 适应场合很多
function randomOne(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
//randomOne([1,2,3,6,8,5,4,2,6])
//2
//randomOne([1,2,3,6,8,5,4,2,6])
//1
A return element 6 array (string) appears次数
function getEleCount (obj, ele) {
var num = 0;
for (var i = 0, len = obj.length; i < len; i++) {
if (ele == obj[i]) {
num++;
}
}
return num;
}
//getEleCount('asd56+asdasdwqe','a')
//3
//getEleCount([1,2,3,4,5,66,77,22,55,22],22)
//2
... not 一一列举
if necessary please 移步
my github ~~
Packaging date and the date string
3-4 // include
a removal string of 空格
four cases
removing all spaces type 1- spaces before and after the space 2- 3- 4- blank spaces before
function trim(str,type){
switch (type){
case 1:return str.replace(/\s+/g,"");
case 2:return str.replace(/(^\s*)|(\s*$)/g, "");
case 3:return str.replace(/(^\s*)/g, "");
case 4:return str.replace(/(\s*$)/g, "");
default:return str;
}
}
2 search string 字段所出现的次数
~
function countStr (str,strSplit){
return str.split(strSplit).length-1
}
3. Date 5-7
Date Date a certain time portion of the time倒计时
function getEndTime(endTime){
var startDate=new Date(); //开始时间,当前时间
var endDate=new Date(endTime); //结束时间,需传入时间参数
var t=endDate.getTime()-startDate.getTime(); //时间差的毫秒数
var d=0,h=0,m=0,s=0;
if(t>=0){
d=Math.floor(t/1000/3600/24);
h=Math.floor(t/1000/60/60%24);
m=Math.floor(t/1000/60%60);
s=Math.floor(t/1000%60);
}
return "剩余时间"+d+"天 "+h+"小时 "+m+" 分钟"+s+" 秒";
}
如需更多觉得`用的多的功能的封装` 欢迎留言/ 大家一起`进步`
Epilogue
This paper lists some common packaging JS, codes distal rope heteroaryl is a major flaw, we hope can learn ideas, packaged together, with progress
goTo--
Welcome to the front direction additive group Q 614569041