栈的实现
//栈的实现
function Stack() {
this.dataStore = [];//初始化数组
this.top = 0;//记录栈顶位置
this.push = push;//入栈
this.pop = pop;//出栈
this.peek = peek;//栈顶元素
this.length = length;//栈内元素
this.clear = clear;//清空
}
//入栈
function push(element) {
this.dataStore[this.top++] = element;
}
//出栈
function pop() {
return this.dataStore[--this.top];
}
//栈顶元素
function peek() {
return this.dataStore[this.top - 1];
}
//栈内元素大小
function length() {
return this.top;
}
//清空
function clear() {
delete this.dataStore;
this.dataStore = [];
this.top = 0;
}
var s = new Stack();
s.push("a");
s.push("b");
s.push('c');
s.push('d');
/// <summary>
/// 数组去重
/// </summary>
/// <param name="fieldName">去重的字段名</param>
Array.prototype.distinct = function (fieldName) {
var arr = this;
var uniqueArr = [];
var includedKey = {};
for (var i = 0; i < arr.length; i++) {
var value = arr[i][fieldName];
if (includedKey[value]) continue;
uniqueArr.push(arr[i]);
includedKey[value] = 1;
}
return uniqueArr;
}
// 测试
var test = [{ id: '1' }, { id: '2' }, { id: '2' }, { id: '1' }, { id: '3' }];
test = test.distinct('id');
alert(JSON.stringify(test));
好好学习,天天向上。

浙公网安备 33010602011771号