最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Java-style Set collection for Javascript - Stack Overflow

programmeradmin1浏览0评论

I need a Set that has the API similar to the Set in Java.

This implementation:

.html

Requires the use of RequireJS, which requires my Java brain to twist too much at the moment.

Posting a function that is the functionality for Set would be a great answer. Or a link to a Google Set or some other tech giant who has created this code already.

What about Google's Closure? The name confused me but it has a set.

I need a Set that has the API similar to the Set in Java.

This implementation:

http://jsclass.jcoglan./set.html

Requires the use of RequireJS, which requires my Java brain to twist too much at the moment.

Posting a function that is the functionality for Set would be a great answer. Or a link to a Google Set or some other tech giant who has created this code already.

What about Google's Closure? The name confused me but it has a set.

Share Improve this question edited Nov 7, 2013 at 3:19 Kevin Panko 8,56419 gold badges51 silver badges63 bronze badges asked Nov 7, 2013 at 1:40 Guido AnselmiGuido Anselmi 3,9228 gold badges37 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

In my opinion whatever java.util.Set can achieve can be done using simple javascript object. I don't see why you need additional library:

// empty set
var basket = {};

// adding object to set    
basket['apple'] = true;
basket['banana'] = true;
basket['orange'] = true;
basket['apple'] = true;

// iterating through set contents, should print:
// apple
// banana
// orange
for(var fruit in basket)
  console.log(fruit);

// check if an element exist
if(basket['pineapple']) {
  console.log('has pineapple');
} else {
  console.log('no pineapple');
}

// remove element from set
delete basket['apple'];
发布评论

评论列表(0)

  1. 暂无评论