在javascript/jQuery中设置Java的数据结构
有没有办法像javascript中的java那样创建集合数据结构(唯一集合)?对于一组字符串,我只会使用值为true的对象在javascript/jQuery中设置Java的数据结构,javascript,jquery,Javascript,Jquery,有没有办法像javascript中的java那样创建集合数据结构(唯一集合)?对于一组字符串,我只会使用值为true的对象 var obj = {}; obj["foo"] = true; obj["bar"] = true; if(obj["foo"]) { // foo in set } 这就是HashSet在Java中的基本工作方式,假设JavaScript对象是作为哈希表实现的(这是典型的)。我已经编写了一个类似于Java的HashSet的哈希集的JavaScript实现。它允许
var obj = {};
obj["foo"] = true;
obj["bar"] = true;
if(obj["foo"])
{
// foo in set
}
这就是HashSet在Java中的基本工作方式,假设JavaScript对象是作为哈希表实现的(这是典型的)。我已经编写了一个类似于Java的HashSet的哈希集的JavaScript实现。它允许将任何对象(不仅仅是字符串)用作集合成员。它基于哈希表的键 我保证,文档很快就会出来。现在,源代码应该非常清楚地向您提供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
虽然这似乎是一个常见的问题,我找到了支持对象的东西,但我想要一个更简单的,最后我自己写了一个。。。如果其他人觉得有用的话
/**
* 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;
};
};
/**
*表示一组唯一值的Javascript类
*
*用法:
*
*var s=新的jsSet();
*
*s.add('a1');s、 添加('a2');
*
*s.list();>>['a1','a2']
*
*s.remove('a1');s、 列表();>>['a2']
*
*s.contains('a1')>>错误
*
*s.contains('a2')>>真
*
*可以用铁链锁住
*添加('hello');
*
*添加数组
*s.addAll([null,'a','b']);
*
*移除阵列
*s.addAll([null,'a','b']);
*
*以列表的形式检索元素
*美国列表();
*
*集合的大小
*美国尺码();
*
*/
函数jsSet(){
//null也可以是集合的一个元素,但需要
//一个单独的指示来区分它
//字符串“null”也是空的
this.isNullAdded=false;
//私有成员变量,因此没有“this”
var-map={};
//优化范围
//可以缓存,而不是每次生成
//this.uniqueList=[];
//如果元素在此集合中,则返回true,否则返回false
this.contains=函数(键){
如果(键===null)
返回此。isNullAdded;
else if(键===未定义)
返回false;
其他的
返回映射[键]?真:假;
};
//将元素添加到集合中
this.add=函数(val){
如果(val==null)
this.isNullAdded=true;
else if(val!==未定义)
map[val]=true;
归还这个;
};
//将数组的所有元素添加到集合中
this.addAll=函数(val){
if(val!==null&&val!==数组的未定义的&&val实例){
对于(var idx=0;idx0?false:true;
};
//以列表形式返回集合的元素
this.list=函数(){
var-arr=[];
如果(添加了this.isNullAdded)
arr.push(空);
用于(地图中的o){
//防止继承属性,例如
//Object.prototype.test='继承的属性';
if(映射hasOwnProperty(o))
arr.push(o);
}
返回arr;
};
};
在现代浏览器中,我只使用键的方法来使用地图。例如:
- 新集合:
让mySet=newmap()代码>
- 添加元素:
mySet.set(元素,1)
- 删除元素:
mySet.delete(元素)
- 包含元素:
mySet.has(元素)
Set
Api,可用于创建唯一的集合
var set = new Set([1,1,2,3]);
set.add(1);
set.add(4);
有关更多信息,请访问-那么如何从该集中删除对象?obj[“foo”]=null?Obj[fo] =假?@ Eran,代码>删除Obj[[ Fo] ] < /> >或代码>删除Obj.Foo< /Calp>。感谢教老狗一个新把戏:这太神奇了,我从未在JS中遇到过,虽然我把我带回了C++的日子……Eran,是的。您可能知道,在JavaScript中这是完全不同的,因为它是垃圾收集的,并且没有析构函数。如果在集合中不需要null值,那么我们可以在添加所有元素后执行remove(null)。