does the java Set data structure exist for nodejs?

0

I was investigating if there is any structure similar to Java SET, which does not allow duplicates in the collection. I need a structure like this to eliminate duplicates of a file

I made this code but it inserts duplicates.

>  function reestructurar(dir,fileA){   var mySet = new Set();
>   read('${dir}/training/${fileA}', contentT => {
>                                   for (var i = 0, chunki = contentT.split('\n'), leni =chunki.length; i < leni; i++){
>                                   mySet.add(chunki);
>                                       
>                               }
>                                   console.log(mySet);
>                               });
> 
> }

modify it like this:

  

var Set = require ("collections / set");

function reestructurar(dir,fileA){
var set = new Set([]);
read('${dir}/training/${fileA}', contentT => {
                            for (var i = 0, chunki = contentT.split('\n'), leni =chunki.length; i < leni; i++){
                                set.add(chunki);

                            }
                                console.log(set);
                        });

}

It does not work, insert everything, I need you to insert not repeated

    
asked by hubman 10.10.2016 в 06:54
source

1 answer

1

Yes, Set and Map among others were added to Javascript in its ES2015 version (also known as ES6).

Its use is simple:

var cartas = new Set()
cartas.add('♠')
cartas.add('♥')
cartas.add('♦')
cartas.add('♣')

cartas.has('♠') // true

cartas.has('joker') // false

cartas.size // 4

cartas.add('♣');

cartas.size // Sigue siendo 4 ya que ♣ ya pertenecía al set.

In this link you have the Set documentation.

To be able to use it you need that nodejs be updated to versions with the V8 engine in order to run ES2015 natively.

To check if your version of node supports it, visit this other link .

    
answered by 11.10.2016 в 13:18