JSは奇淫巧技を操作して、絶えず記録学習します.

1078 ワード

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, "");};