Converts the array to a "set" - Node.js Data Structure

Node.js examples for Data Structure:Set

Description

Converts the array to a "set"

Demo Code

  /**//from w  w w .  ja v a 2  s .c o m
   * Converts the array to a "set": an object whose keys are the original
   * array's values and whose values are all true. This allows efficient
   * membership testing of the array when it needs to be done repeatedly.
   */
  to_set: function array_to_set(array)
  {
    var s = {};
    var len = array.length;
    
    for (var i = 0; i < len; i++) {
      if (i in array)
        s[array[i]] = true;
    }
    
    return s;
  },

Related Tutorials