赞
踩
作为前端开发,JS是重中之重,最近结束了面试的高峰期,基本上offer也定下来了就等开奖,趁着这个时间总结下32个手写JS问题,这些都是高频面试题,希望对你能有所帮助。
关于源码都紧遵规范,都可跑通MDN示例,其余的大多会涉及一些关于JS的应用题和本人面试过程
数组扁平化是指将一个多维数组变为一个一维数组
- const arr = [1, [2, [3, [4, 5]]], 6];
- // => [1, 2, 3, 4, 5, 6]
const res1 = arr.flat(Infinity);
const res2 = JSON.stringify(arr).replace(/\[|\]/g, '').split(',');
但数据类型都会变为字符串
const res3 = JSON.parse('[' + JSON.stringify(arr).replace(/\[|\]/g, '') + ']');
- const flatten = arr => {
- return arr.reduce((pre, cur) => {
- return pre.concat(Array.isArray(cur) ? flatten(cur) : cur);
- }, [])
- }
- const res4 = flatten(arr);
- const res5 = [];
- const fn = arr => {
- for (let i = 0; i < arr.length; i++) {
- if (Array.isArray(arr[i])) {
- fn(arr[i]);
- } else {
- res5.push(arr[i]);
- }
- }
- }
- fn(arr);
- const arr = [1, 1, '1', 17, true, true, false, false, 'true', 'a', {}, {}];
- // => [1, '1', 17, true, false, 'true', 'a', {}, {}]
const res1 = Array.from(new Set(arr));
- const unique1 = arr => {
- let len = arr.length;
- for (let i = 0; i < len; i++) {
- for (let j = i + 1; j < len; j++) {
- if (arr[i] === arr[j]) {
- arr.splice(j, 1);
- // 每删除一个树,j--保证j的值经过自加后不变。同时,len--,减少循环次数提升性能
- len--;
- j--;
- }
- }
- }
- return arr;
- }
- const unique2 = arr => {
- const res = [];
- for (let i = 0; i < arr.length; i++) {
- if (res.indexOf(arr[i]) === -1) res.push(arr[i]);
- }
- return res;
- }
当然也可以用include、filter,思路大同小异。
- const unique3 = arr => {
- const res = [];
- for (let i = 0; i < arr.length; i++) {
- if (!res.includes(arr[i])) res.push(arr[i]);
- }
- return res;
- }
- const unique4 = arr => {
- return arr.filter((item, index) => {
- return arr.indexOf(item) === index;
- });
- }
- const unique5 = arr => {
- const map = new Map();
- const res = [];
- for (let i = 0; i < arr.length; i++) {
- if (!map.has(arr[i])) {
- map.set(arr[i], true)
- res.push(arr[i]);
- }
- }
- return res;
- }
类数组是具有length属性,但不具有数组原型上的方法。常见的类数组有arguments、DOM操作方法返回的结果。
Array.from(document.querySelectorAll('div'))
Array.prototype.slice.call(document.querySelectorAll('div'))
[...document.querySelectorAll('div')]
Array.prototype.concat.apply([], document.querySelectorAll('div'));
- Array.prototype.filter = function(callback, thisArg) {
- if (this == undefined) {
- throw new TypeError('this is null or not undefined');
- }
- if (typeof callback !== 'function') {
- throw new TypeError(callback + 'is not a function');
- }
- const res = [];
- // 让O成为回调函数的对象传递(强制转换对象)
- const O = Object(this);
- // >>>0 保证len为number,且为正整数
- const len = O.length >>> 0;
- for (let i = 0; i < len; i++) {
- // 检查i是否在O的属性(会检查原型链)
- if (i in O) {
- // 回调函数调用传参
- if (callback.call(thisArg, O[i], i, O)) {
- res.push(O[i]);
- }
- }
- }
- return res;
- }

- Array.prototype.map = function(callback, thisArg) {
- if (this == undefined) {
- throw new TypeError('this is null or not defined');
- }
- if (typeof callback !== 'function') {
- throw new TypeError(callback + ' is not a function');
- }
- const res = [];
- // 同理
- const O = Object(this);
- const len = O.length >>> 0;
- for (let i = 0; i < len; i++) {
- if (i in O) {
- // 调用回调函数并传入新数组
- res[i] = callback.call(thisArg, O[i], i, this);
- }
- }
- return res;
- }

forEach
跟map类似,唯一不同的是
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。