手写题该怎么准备, 数组去重有哪些写法?
常见问题
- 手写题库 https://github.com/Mayandev/fe-interview-handwrite
- 数组去重有哪些方式?手写数组去重
概念索引
- 题库: 按题型归类的手写代码集合, 用于对照自测;
- 去重: 保留唯一值, 关键是先定判等口径;
- SameValueZero: Set 与 includes 用的判等, 让 NaN 等于自身;
- 引用类型: 对象按地址判等, 内容相同也算两个;
- 手写工具: map、filter、reduce、flat 是最常被要求重写的;
- call 与 apply: 手写 bind 的基础, 决定 this 落到哪个对象;
题库怎么用?
- 来源: fe-interview-handwrite 按题型分类;
- 顺序: 先数组与字符串方法, 再 this 相关, 最后 Promise 类;
- 方法: 每题先写边界与预期, 再写实现, 最后跑用例;
- 验收: 能解释每一行的目的才算会, 背下来不算;
数组去重有哪些方式?
| 写法 | 判等口径 | 说明 |
|---|---|---|
[...new Set(arr)] | SameValueZero | 最短, 能去掉 NaN |
arr.filter 加 indexOf | 严格相等 | 保留首次出现, 但 NaN 全丢 |
reduce 加 includes | SameValueZero | 兼容老环境 |
| 双重循环加 splice | 严格相等 | 面试里最常见的手写 |
| Map 记标记 | SameValueZero | 大数组时线性最快 |
const arr = [1, 2, 1, NaN, NaN, { a: 1 }, { a: 1 }];
[...new Set(arr)]; // [1, 2, NaN, {a:1}, {a:1}]
arr.filter((v, i) => arr.indexOf(v) === i); // 两个 NaN 都被丢掉
arr.reduce((acc, v) => (acc.includes(v) ? acc : [...acc, v]), []);
// [1, 2, NaN, {a:1}, {a:1}]; 去掉 NaN 重复, 但对象按地址算
- 对象: 内容相同地址不同, 都会被保留; 要按 id 去重得自己写比较;
- 字符串: 大小写与首尾空白应先统一, 否则会被当成不同值;
手写去重要注意什么?
- 判等: indexOf 用严格相等, 认不出 NaN, 所以 filter 版会把 NaN 全丢掉;
- Set: 用 SameValueZero, NaN 等于自身, 通常直接选它;
- 顺序: 各写法都保留首次出现的顺序, 这一点要能说清;
- 复杂度: 双重循环是平方级, Map 与 Set 是线性;
function unique(arr) {
if (!Array.isArray(arr)) return []; // 边界: 非数组直接返回空
const seen = new Map(); // SameValueZero 判等
const result = [];
for (const item of arr) {
if (seen.has(item)) continue; // 出现过就跳过
seen.set(item, true);
result.push(item);
}
return result; // 保持原顺序, O(n)
}
手写数组方法怎么下手?
- 共同结构: 新建容器, 遍历 this, 按语义决定往容器里放什么;
- map: 用回调返回值填新数组, 长度不变;
- filter: 回调为真才收进新数组;
- reduce: 有初值从第 0 项累加, 没有初值就拿第 0 项当初始值;
- flat: 递归摊平, 用深度计数控制层数;
Array.prototype.myMap = function (fn, thisArg) {
const out = [];
for (let i = 0; i < this.length; i++) out.push(fn.call(thisArg, this[i], i, this));
return out;
};
Array.prototype.myReduce = function (fn, init) {
let i = 0;
let acc = init;
if (acc === undefined) { acc = this[0]; i = 1; } // 边界: 无初值时用首项
for (; i < this.length; i++) acc = fn(acc, this[i], i, this);
return acc;
};
- 稀疏数组: 原生 map 会跳过空位, 简化版通常不处理, 面试时可以主动说明;
手写 call 与 bind 用什么思路?
- call: 把函数临时挂到目标对象上调用, 用完删掉;
- apply: 同上, 只是实参以数组传入;
- bind: 返回新函数, 内部用 apply 固定 this 并拼接预置参数;
- 边界: 目标为 null 时退到全局对象, 这是非严格模式的行为;
Function.prototype.myCall = function (ctx, ...args) {
const target = ctx == null ? globalThis : Object(ctx); // 边界: null 退到全局
const key = Symbol("fn"); // 用 Symbol 避免覆盖已有属性
target[key] = this;
const ret = target[key](...args);
delete target[key]; // 用完清理, 别留下痕迹
return ret;
};
Function.prototype.myBind = function (ctx, ...preset) {
const fn = this;
return function (...args) {
return fn.apply(ctx, preset.concat(args)); // 预置参数在前
};
};
- 原型与 new: 这两块自己在原型那一篇写一遍, 能顺带讲清原型链;
Promise 类工具怎么写?
- all: 计数, 全部兑现后按原下标返回结果数组;
- 失败: 任意一项拒绝就整体拒绝, 不再等其它项;
- 空数组: 直接兑现成空数组;
- 说明: Promise 状态与事件循环在异步一篇, 这里只写聚合逻辑;
function promiseAll(list) {
return new Promise((resolve, reject) => {
const results = new Array(list.length);
let left = list.length;
if (left === 0) return resolve(results); // 边界: 空数组立即兑现
list.forEach((p, i) => {
Promise.resolve(p).then((v) => {
results[i] = v; // 按原下标放, 保证顺序
if (--left === 0) resolve(results);
}, reject); // 任一项失败整体失败
});
});
}
- 顺序: 结果顺序由下标决定, 与各项完成的先后无关;