自定义随机范围的随机函数
function rand(a,b){//产生随机数字 a是最大数字,b是最小数字(不包括a,b)
var Num = parseInt(Math.random() * (a - b + 1) + b);
return Num;
}
Object对象无法返回长度
Object.keys(obj).length //对象长度
Object.keys(obj)
Object.keys("foo");
// TypeError: "foo" is not an object (ES5 code)
Object.keys("foo");
// ["0", "1", "2"] (ES2015 code)
数组之间处理,交集并集差集
// 并集
let union = a.concat(b.filter(v => !a.includes(v))) // [1,2,3,4,5]
// 交集
let intersection = a.filter(v => b.includes(v)) // [2]
// 差集
let difference = a.concat(b).filter(v => !a.includes(v) || !b.includes(v)) // [1,3,4,5]
求和
function sum(_array){
return _array.reduce(function(prev, curr, idx, arr){
return prev + curr;
})
}