共计 734 个字符,预计需要花费 2 分钟才能阅读完成。
1、数组降维度
二维数组
let arr = [[1], [2], [3] ];
arr = Array.prototype.concat.apply([], arr);
console.log(arr);// [1, 2, 3]
let array = [[1], [2], [3] ];
array = array.flat(2);
console.log(array); // [1, 2, 3]
多维数组
let arrMore = [1, 2, [3], [[4]]];
arrMore = arrMore.flat(Infinity);
console.log(arrMore);
2、判断小数是否相等
function equal(number1, number2) {return Math.abs(number1 - number2) < Math.pow(2, -52);
}
console.log(equal(0.1 + 0.2, 0.3));
3、随机生成字母和数组的组合
Math.random().toString(36).substr(2);
4、合并对象
const person = {name: 'David Walsh', gender: 'Male'};
const tools = {computer: 'Mac', editor: 'Atom'};
const attributes = {handsomeness: 'Extreme', hair: 'Brown', eyes: 'Blue'};
const summary = {...person, ...tools, ...attributes};
console.log(summary);
5、字符串去空格
String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g, "");};
正文完