有没有什么方法可以在javascript中创建像java一样的设置数据结构(Unique Collections)?在javascript/jQuery中设置Java的数据结构
22
A
回答
45
对于一组字符串,我只是使用一个值为true的对象。
var obj = {};
obj["foo"] = true;
obj["bar"] = true;
if(obj["foo"])
{
// foo in set
}
这基本上是HashSet的是如何工作的在Java中,假设JavaScript对象作为一个哈希表(这是典型的)来实现。
9
我已经编写了一个类似于Java的HashSet的哈希集合的JavaScript实现。它允许任何对象(不只是字符串)被用作集合成员。它基于散列表的键。
http://code.google.com/p/jshashtable/downloads/list
文件将很快宣布,我保证。目前,源应该给你的API很清楚,这里是一个例子:
var s = new HashSet();
var o1 = {name: "One"}, o2 = {name: "Two"};
s.add(o1);
s.add(o2);
s.add(o2);
s.values(); // Array containing o1 and a single reference to o2
1
好吧,虽然这似乎是一个常见的问题,我发现了似乎是a good Set class on the net支持对象,我想要一个更简单的一个,最后写一个自己...以防其他人发现它有用...
/**
* A Javascript Class that represents a set of unique values
*
* Usage:
*
* var s = new jsSet();
*
* s.add('a1'); s.add('a2');
*
* s.list(); >> ['a1','a2']
*
* s.remove('a1'); s.list(); >> ['a2']
*
* s.contains('a1') >> false
*
* s.contains('a2') >> true
*
* can be chained
* s.add(null).add('hello');
*
* add array
* s.addAll([ null, 'a', 'b' ]);
*
* remove array
* s.addAll([ null, 'a', 'b' ]);
*
* retrieve the elements as a list
* s.list();
*
* size of the set
* s.size();
*
*/
function jsSet() {
// null can also be an element of the set, but needs
// a separate indication to differentiate it from
// the string "null" as well
this.isNullAdded = false;
// private member variable hence no 'this'
var map = {};
// Scope for optimization
// could be cached instead of generating each time
// this.uniqueList = [];
// returns true if the element is in this set, false otherwise
this.contains = function(key) {
if (key === null)
return this.isNullAdded;
else if (key === undefined)
return false;
else
return map[key] ? true : false;
};
// adds the element to the set
this.add = function(val) {
if (val === null)
this.isNullAdded = true;
else if (val !== undefined)
map[val] = true;
return this;
};
// adds all the elements of the array to the set
this.addAll = function(val) {
if (val !== null && val !== undefined && val instanceof Array) {
for (var idx = 0; idx < val.length; idx++) {
this.add(val[idx]);
}
}
return this;
};
// removes the specified element from the set
this.remove = function(val) {
if (val === null)
this.isNullAdded = false;
else if (val !== undefined)
delete map[val];
return this;
};
// removes all the element in the array from the set
this.removeAll = function(val) {
if (val !== null && val !== undefined && val instanceof Array) {
for (var idx = 0; idx < val.length; idx++) {
console.log('val: %s:%s', idx, val[idx]);
this.remove(val[idx]);
}
}
return this;
};
// empties the set of all values
this.clear = function() {
this.isNullAdded = false;
map = {};
return this;
};
// returns the number of elements in the set
this.size = function() {
return this.list().length;
};
// returns true if the set is empty, false otherwise
this.isEmpty = function() {
return this.list().length > 0? false: true;
};
// returns the elements of the set as a list
this.list = function() {
var arr = [];
if (this.isNullAdded)
arr.push(null);
for (o in map) {
// protect from inherited properties such as
// Object.prototype.test = 'inherited property';
if (map.hasOwnProperty(o))
arr.push(o);
}
return arr;
};
};
+0
如果我们在Set中不需要空值,那么我们可以在添加所有元素之后执行remove(null)。 – msanjay 2011-11-04 11:40:33
相关问题
- 1. 如何在Java中实现设置数据结构?
- 2. 设计数据结构/ Java数据结构
- 3. 在Java中使用的数据结构
- 4. Java数据结构
- 5. Java数据结构
- 6. 使用fscanf在结构数组中设置结构的属性
- 7. 在Java的构造函数中使用设置方法设置数据?
- 8. 在Java中创建类似于数据结构的结构
- 9. 的Java HasMap数据结构
- 10. 在java中实现数据结构
- 11. 在Java中映射数据结构
- 12. 设定数组值设置在结构
- 13. 结构中的整数设置为零
- 14. 将数据放置在matlab结构中?
- 15. 的设置结构
- 16. Java中的Trie数据结构
- 17. Java中的分布式数据结构
- 18. 像java中的数据结构树
- 19. Java中contains()的最快数据结构?
- 20. Java中的复杂数据结构
- 21. Java中的持久数据结构
- 22. Java中的Class Dictionary(数据结构)
- 23. 难看Java数据结构
- 24. Java对象 - 数据结构
- 25. 结构java树型数据
- 26. 查询Java数据结构
- 27. Java Tree数据结构
- 28. Java数据结构表示
- 29. Java数据结构问题
- 30. 数据结构用java
那么如何从该设置中删除一个对象? OBJ [ “foo” 的] =空?或obj [“foo”] = false? – 2011-04-05 13:47:51
@Eran,'删除obj [“foo”]'或'删除obj.foo'。 – 2011-04-05 13:53:37
感谢教老狗一个新的诀窍:)这是惊人的,我从来没有在JS遇到它,把我带回到我的C++日子虽然... – 2011-04-05 16:18:08