ES6 原生提供 Map/Set,但在不支援 ES6 的老環境(IE8-、舊版 WebView)或需自定義行為時,手寫一個符合習慣接口的 Map/List 仍有價值。本文提供功能完整的 ES5 實現,含增刪改查、鍵值遍歷、判空、清空等完整 API。
function Map() {
this.elements = [];
// 返回元素數量
this.size = function() { return this.elements.length; };
// 判斷是否為空
this.isEmpty = function() { return this.elements.length < 1; };
// 清空所有元素
this.clear = function() { this.elements = []; };
// 新增/更新 鍵值對
this.put = function(key, value) {
// 先移除舊鍵(保證鍵唯一)
this.remove(key);
this.elements.push({ key: key, value: value });
};
// 移除指定鍵,成功返回 true
this.remove = function(key) {
for (var i = 0; i < this.elements.length; i++) {
if (this.elements[i].key === key) {
this.elements.splice(i, 1);
return true;
}
}
return false;
};
// 獲取指定鍵的值,不存在返回 null
this.get = function(key) {
for (var i = 0; i < this.elements.length; i++) {
if (this.elements[i].key === key) return this.elements[i].value;
}
return null;
};
// 獲取指定索引的元素對象 {key, value}
this.element = function(index) {
if (index < 0 || index >= this.elements.length) return null;
return this.elements[index];
};
// 判斷是否包含鍵
this.containsKey = function(key) {
for (var i = 0; i < this.elements.length; i++)
if (this.elements[i].key === key) return true;
return false;
};
// 判斷是否包含值
this.containsValue = function(value) {
for (var i = 0; i < this.elements.length; i++)
if (this.elements[i].value === value) return true;
return false;
};
// 返回所有值數組
this.values = function() {
var arr = [];
for (var i = 0; i < this.elements.length; i++) arr.push(this.elements[i].value);
return arr;
};
// 返回所有鍵數組
this.keys = function() {
var arr = [];
for (var i = 0; i < this.elements.length; i++) arr.push(this.elements[i].key);
return arr;
};
}
var map = new Map();
map.put("name", "張三");
map.put("age", 25);
map.put("city", "北京");
console.log(map.get("name")); // "張三"
console.log(map.size()); // 3
console.log(map.containsKey("age")); // true
console.log(map.keys()); // ["name", "age", "city"]
console.log(map.values()); // ["張三", 25, "北京"]
map.remove("age");
console.log(map.size()); // 2
// 遍歷
for (var i = 0; i < map.size(); i++) {
var elem = map.element(i);
console.log(elem.key + " = " + elem.value);
}
function List() {
this.elements = [];
this.add = function(val) { this.elements.push(val); };
this.addAt = function(index, val) { this.elements.splice(index, 0, val); };
this.get = function(index) { return (index >=0 && index < this.elements.length) ? this.elements[index] : null; };
this.remove = function(index) { return this.elements.splice(index, 1)[0] || null; };
this.removeValue = function(val) { var i = this.elements.indexOf(val); return i>-1 ? this.elements.splice(i,1)[0] : null; };
this.size = function() { return this.elements.length; };
this.isEmpty = function() { return this.elements.length === 0; };
this.clear = function() { this.elements = []; };
this.contains = function(val) { return this.elements.indexOf(val) > -1; };
this.toArray = function() { return this.elements.slice(); };
this.indexOf = function(val) { return this.elements.indexOf(val); };
this.lastIndexOf = function(val) { return this.elements.lastIndexOf(val); };
}
Map/Set/Array**,性能優化極致且 API 標準。core-js 或 babel-polyfill 打補丁,避免維護自定義實現。Map 已保證順序,無需自行實現。理解底層數據結構實現有助於面試與造輪子,生產代碼請優先拥抱標準庫。