简体   繁体   English

如何使用动态数据实现javascript Map结构?

[英]How implement javascript Map structure with dynamic data ?

I am trying to create tree like component, for the first level data is coming from the server , if the user clicks the node i need to populate the child nodes with the data from service call. 我正在尝试创建树状组件,因为第一级数据来自服务器,如果用户单击该节点,则需要使用服务调用中的数据填充子节点。

what is the best way to save the data for this tree component ? 保存此树组件数据的最佳方法是什么? because user will do some operations on the tree component like remove, add & move. 因为用户将对树组件执行某些操作,例如删除,添加和移动。 Finally i need to send the updated data to the server . 最后,我需要将更新的数据发送到服务器。

This is the hashmap functionality I use in javascript. 这是我在JavaScript中使用的哈希图功能。 I based it off the docs of java 7 hashmap. 我基于Java 7 hashmap的文档。 http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html

I added the load and save variables to allow JSON storage. 我添加了load和save变量以允许JSON存储。 be careful though. 但是要小心。 if you stored any complex objects(like a hashmap in a hashmap) you will lose that. 如果您存储了任何复杂的对象(例如哈希图中的哈希图),则将丢失该对象。 You'd have to implement your own object instantiatiors in the load and save function. 您必须在load和save函数中实现自己的对象实例化。

A JSfiddle to play with if you like: http://jsfiddle.net/mdibbets/s51tubm4/ 如果您愿意,可以使用JSfiddle: http : //jsfiddle.net/mdibbets/s51tubm4/

 function HashMap() { this.map = {}; this.listsize = 0; } HashMap.prototype._string = function(key) { if(typeof key.toString !== 'undefined') { return key.toString(); } else { throw new Error('No valid key supplied. Only supply Objects witha toString() method as keys'); } } HashMap.prototype.put = function(key,value) { key = this._string(key); if(typeof this.map[key] === 'undefined') { this.listsize++; } this.map[key] = value; } HashMap.prototype.get = function(key) { key = this._string(key); return this.map[key]; } HashMap.prototype.containsKey = function(key) { key = this._string(key); return !(this.map[key] === 'undefined'); } HashMap.prototype.putAll = function(hashmap) { if(hashmap instanceof HashMap) { var othermap = hashmap.map; for(var key in othermap) { if(othermap.hasOwnProperty(key)) { if(typeof this.map[key] === 'undefined') { this.listsize++; } this.map[key] = othermap[key]; } } } else { throw new Error('No HashMap instance supplied'); } } HashMap.prototype.remove = function(key) { key = this._string(key); var ret = null; if(typeof this.map[key] !== 'undefined') { ret = this.map[key]; delete this.map[key]; this.listsize--; } return ret; } HashMap.prototype.clear = function() { this.map = {}; this.listsize = 0; } HashMap.prototype.containsValue = function(value) { for(var key in this.map) { if(this.map.hasOwnProperty(key)) { if(this.map[key] === value) { return true; } } } return false; } HashMap.prototype.clone = function() { var ret = new HashMap(); ret.map = this.map; ret.listsize = this.listsize; return ret; } HashMap.prototype.entrySet = function() { return this.map; } HashMap.prototype.keySet = function() { var ret = []; for(var key in this.map) { if(this.map.hasOwnProperty(key)) { ret.push(key); } } return ret; } HashMap.prototype.values = function() { var ret = []; for(var key in this.map) { if(this.map.hasOwnProperty(key)) { ret.push(this.map[key]); } } return ret; } HashMap.prototype.size = function(activeCheck) { //Active check is expensive. if(typeof activeCheck !== 'undefined' && activeCheck) { var count = 0; for(var key in this.map) { if(this.map.hasOwnProperty(key)) { count++; } } return count; } return this.listsize; } HashMap.prototype.save = function(){ return JSON.stringify(this.map); } HashMap.prototype.load = function(json) { if(typeof json !== 'string') { throw new Error("No valid input supplied. Only supply JSON Strings"); } this.map = JSON.parse(json); this.listsize = this.size(true); } var map = new HashMap(); console.log( map.put('hello', true), map.get('hello'), map.put('hello',10), map.put('world',20), map.values(), map.keySet(), map.entrySet(), map.containsValue('twoshoes'), map.size() ); var map2 = new HashMap(); map2.put('goody','twoshoes'); map2.putAll(map); console.log( map2.get('hello'), map2.values(), map2.keySet(), map2.entrySet(), map2.containsValue('twoshoes'), map2.size() ); var map3 = new HashMap(); map3.load(map2.save()); console.log( map3.get('hello'), map3.values(), map3.keySet(), map3.entrySet(), map3.containsValue('twoshoes'), map3.size() ); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM