function SortMap() {
this.keys = new Array();
this.data = new Map();
this.propKey = '';
this.propValueKey = '';
this.init = function (initArray,propKey,propValueKey) {
this.propKey = propKey
this.propValueKey = propValueKey
initArray.forEach((item,index) => {
this.set(item[propKey],item[propValueKey])
});
return this;
};
this.set = function (key, value) {
if (this.data[key] == null) {
this.keys.push(key);
}
this.data[key] = value;
return this;
};
this.insertByIndex = function (obj,index) {
let key = obj[this.propKey]
let value = obj[this.propValueKey]
this.keys.splice(index,0,key);
this.data[key] = value;
return this;
};
this.removeByIndex = function (index) {
let key = this.keys[index];
this.keys.splice(index,1);
this.data[key] = null;
};
this.get = function (key) {
return this.data[key];
};
this.remove = function (key) {
let index = this.keys.indexOf(key)
this.keys.splice(index,1);
this.data[key] = null;
};
this.isEmpty = function () {
return this.keys.length == 0;
};
this.size = function () {
return this.keys.length;
};
this.each = function(fn){
if(typeof fn != 'function'){
return;
}
var len = this.keys.length;
for(var i=0;i<len;i++){
var k = this.keys[i];
fn(k,this.data[k],i);
}
};
this.entrys = function() {
var len = this.keys.length;
var entrys = new Array(len);
for (var i = 0; i < len; i++) {
entrys[i] = {
key : this.keys[i],
value : this.data[this.keys[i]]
};
}
return entrys;
};
this.toString = function(){
var s = "{";
for(var i=0;i<this.keys.length;i++,s+=','){
var k = this.keys[i];
s += k+"="+this.data[k];
}
if(s.length > 0){
s = s.substring(0,s.length-1)
}
s+="}";
return s;
};
}
function testMap(){
var m = new SortMap();
let arr = [
{prop: 'craftFlowNum', label: '工序流', width: '46'},
{prop: 'craftCode', label: '工序编码', width: '90'},
{prop: 'craftPartName', label: '结构部件', width: '86'},
]
m.init(arr,'prop','width')
m.insertByIndex( {prop: 'xxx', label: 'xxx', width: '100'},2)
console.log("init:"+m);
m.removeByIndex(0)
console.log("init:"+m);
}
testMap()