前端笔试题

107 阅读1分钟

数组扁平化

const testArray = [1, [2, [3, [4, [5, [6, [7, [[[[[[8, ['ha']]]]]]]]]]]]]]; 
const resultArray = testArray.flat(Infinity); 
console.log(resultArray);  // [ 1, 2, 3, 4, 5, 6, 7, 8, 'ha']

数组排序

var arr = [1, 9, 4, 50, 49, 6, 3, 2];
function test(){
  return arr.sort(sortNumber);
}
function sortNumber(a, b){
  return a - b;
}
test();

数组去重

var arr = [1, 1, 4, 50, 50, 6, 2, 2];
function unique(arr){
  return Array.from(new Set(arr));
}
const a = unique(arr);
console.log(a);

垂直居中css方法

display:flex;
align-items: center;
justify-content: center;

翻转字符串

let str="hello word"; 
let b=[...str].reverse().join(""); //drow olleh

0.1+0.2

let a = 0.1;
let b = 0.2;
let c = (a+b).toFixed(2)