Skip to content

常用代码技巧 #18

@CodeDreamfy

Description

@CodeDreamfy

生成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
}+

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions