Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

常用代码技巧 #18

Open
CodeDreamfy opened this issue Apr 20, 2018 · 0 comments
Open

常用代码技巧 #18

CodeDreamfy opened this issue Apr 20, 2018 · 0 comments

Comments

@CodeDreamfy
Copy link
Owner

CodeDreamfy commented Apr 20, 2018

生成a-z字母

new Array(26).fill(97).map((item, index)=>String.fromCharCode(item+index))
// ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]

去掉数组中值为false的值

const arr = [1,0,'',undefined,null];
arr.filter(Boolean);
// [1]

数组去重 只针对数字或者字符串

let arr = [1,2,2,3,4,4];
arr = [...new Set(arr)];
// [1,2,3,4]

指定随机数

function selectFrom(lowerValue, upperValue){
  var choices = upperValue - lowerValue + 1;
  return Math.floor(Math.random()*choices + lowerValue);
}

比较数组钟的最大值最小值

Math.min.apply(null, array); // min
Math.max.apply(nullm array); // max
Math.max(...array);

获取全局global对象

var global = function(){
  return this
}+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant