function Map() {
var struct = function(key, value) {
this.key = key;
this.value = value;
}
var put = function(key, value){
for (var i = 0; i < this.arr.length; i++) {
if ( this.arr[i].key === key ) {
this.arr[i].value = value;
return;
}
}
this.arr[this.arr.length] = new struct(key, value);
}
var get = function(key) {
for (var i = 0; i < this.arr.length; i++) {
if ( this.arr[i].key === key ) {
return this.arr[i].value;
}
}
return null;
}
var remove = function(key) {
var v;
for (var i = 0; i < this.arr.length; i++) {
v = this.arr.pop();
if ( v.key === key ) {
continue;
}
this.arr.unshift(v);
}
}
var size = function() {
return this.arr.length;
}
var isEmpty = function() {
return this.arr.length <= 0;
}
this.arr = new Array();
this.get = get;
this.put = put;
this.remove = remove;
this.size = size;
this.isEmpty = isEmpty;
}
// --></mce:script>
<mce:script type="text/javascript"><!--
var map = new Map();
map.put("re","redhacker");
map.put("do","douguoqiang");
map.put("gq","dougq");
alert("map的大小为:" + map.size())
alert("key为re的map中存储的对象为:" + map.get("re"));
map.remove("re");
alert("移除key为re的对象后,获取key为re的map中存储的对象为:" + map.get("re"));
alert("map移除一个元素后的大小为:" + map.size());
alert("map是否是一个空map:" + map.isEmpty());
// --></mce:script>
js实现set集合
<script type="text/javascript">
Array.prototype.distinct = function() {
var x = [], r = [];
for(var i = 0; i < this.length; i++) {
x['_' + this[i]] = this[i];
}
for(var b in x) {
if(typeof x[b] != 'function') {
r.push(x[b]);
}
}
return r;
}
window.onload = function() {
var a = [ 1, 4, 2, 4, 3, 8, 10, 3, 9, 1, 9, 7, 111 ];
var r = a.distinct();
alert(a.join(','));
alert(r.join(','));
}
</script>
JS判断数组是否重复
var ary = new Array("111","22","33","111");
var nary=ary.sort();
for(var i=0;i<ary.length;i++){
if (nary[i]==nary[i+1]){
alert("数组重复内容:"+nary[i]);
}
}