2015-06-15 148 views
0

工作在Holdem手评估器上,部分牦牛剃须正在编写“你从7张牌获得5张组合”函数(pickNofSet())。我已经这样做了,但是我做的这种方式会返回一堆重复项。代码适用于数组,但不适用于多维数组。

所以我必须写一个removeDuplicates()。这就是问题......它使用一个简单的数组,但它不适用于我的“pickNofSet”函数生成的“数组数组”。

- 这里的removeDuplicates码 -

var removeDuplicates = function(input){ // takes array 
var output = []; 
for (i=0; i < input.length; i++){ 
    var unique = true; // all elements are innocent until proven guilty 
    for(j=i+1; j < input.length; j++){ 
     if(input[j] === input[i]){ 
      unique = false; // guilty! 
     };// endif 
    };// end jfor 
    if(unique){ // if not found guilty, 
     output.push(input[i]); // you may go free, little element 
    };// end if 
};// end ifor 
console.log(output); 
return output; };//end function 

这里就是我从控制台得到:

> removeDuplicates(['a','b','c'],['a','b','c'],['d','e','f'],['g','h','i']); 
< undefined 
> removeDuplicates([1, 2, 2, 3, 3, 5, 5, 6, 6, 6]); 
< [1, 2, 3, 5, 6] 
+3

运算符'==='不能用于比较两个阵列。这就是为什么它使用数字元素,但不是数组元素。 –

+0

您没有将它传递给多维数组,您将它作为单独的参数传递给几个不同的数组。所以你的第一个调用不应该返回'undefined',它应该在第一个参数中处理数组并返回'[“a”,“b”,“c”]'。 (我知道这不是你想要的,但你显示的输出与该代码应该发生的不匹配。) – nnnnnn

+0

谢谢你们!我自己从来不会在一百万年内得到这个! –

回答

0

操作===无法比拟的阵列

由于您使用===来检查两个元素之间的相等性,这只适用于原始数据类型,但不适用于数组。例如

1===1 // true 
[1]===[1] // Sorry, you can't 

如果你希望你的算法与两个基本元素和数组元素的工作,你可能需要从===升级平等检查,以自定义的功能。

从这:

if(input[j] === input[i]){ 
    unique = false; // guilty! 
};// endif 

向该:

if (equals(input[j],input[i]){ 
    unique = false; // guilty! 
};// endif 

而实现equals功能以能够比较两个基本数据类型和数组:

function equals(a,b){ 
    if (typeof(a)!=typeof(b)) 
     return false; 
    else if (typeof(a)=='object'){ 
     if (Object.keys(a).length != Object.keys(b).length) 
      return false; 
     else{ 
      for (var keyA of Object.keys(a)){ 
       if (!(keyA in b)) 
        return false; 
       else if (a[keyA]!==b[keyA]) 
        return false; 
       else 
        return true; 
      } 
     } 
    } 
    else 
     return a===b; 
} 

提示:该解决方案应该,也希望,也可以使用JSON元素。

0

下面是一个通用的唯一过滤器示例,应该满足您的需求。需要符合ES5的环境。

(function() { 
 
    'use strict'; 
 

 
    function $strictEqual(a, b) { 
 
     return a === b; 
 
    } 
 

 
    function $isUndefined(inputArg) { 
 
     return $strictEqual(typeof inputArg, 'undefined'); 
 
    } 
 

 
    function $isPrimitive(inputArg) { 
 
     var type = typeof inputArg; 
 

 
     return type === 'undefined' || inputArg === null || type === 'boolean' || type === 'string' || type === 'number' || type === 'symbol'; 
 
    } 
 

 
    function $isFunction(inputArg) { 
 
     return typeof inputArg === 'function'; 
 
    } 
 

 
    function $isDate(inputArg) { 
 
     return Object.prototype.toString.call(inputArg) === '[object Date]'; 
 
    } 
 

 
    function $isRegExp(inputArg) { 
 
     return Object.prototype.toString.call(inputArg) === '[object RegExp]'; 
 
    } 
 

 
    function $isString(inputArg) { 
 
     return Object.prototype.toString.call(inputArg) === '[object String]'; 
 
    } 
 

 
    function $isArguments(inputArg) { 
 
     return Object.prototype.toString.call(inputArg) === '[object Arguments]'; 
 
    } 
 

 
    function $getItem(object, index) { 
 
     var item; 
 

 
     if ($isString(object)) { 
 
      item = object.charAt(index); 
 
     } else { 
 
      item = object[index]; 
 
     } 
 

 
     return item; 
 
    } 
 

 
    var de = function (a, b, circ) { 
 
     if (a === b) { 
 
      return true; 
 
     } 
 

 
     var aType, 
 
      bType, 
 
      aIsArgs, 
 
      bIsArgs, 
 
      aIsPrim, 
 
      bIsPrim, 
 
      aCirc, 
 
      bCirc, 
 
      ka, 
 
      kb, 
 
      length, 
 
      index, 
 
      it; 
 

 
     if ($isDate(a) && $isDate(b)) { 
 
      return a.getTime() === b.getTime(); 
 
     } 
 

 
     if ($isRegExp(a) && $isRegExp(b)) { 
 
      return a.source === b.source && 
 
       a.global === b.global && 
 
       a.multiline === b.multiline && 
 
       a.lastIndex === b.lastIndex && 
 
       a.ignoreCase === b.ignoreCase && 
 
       a.sticky === b.sticky; 
 
     } 
 

 
     aIsPrim = $isPrimitive(a); 
 
     bIsPrim = $isPrimitive(b); 
 
     if ((aIsPrim || $isFunction(a)) && (bIsPrim || $isFunction(b))) { 
 
      /*jslint eqeq: true */ 
 
      return a == b; 
 
     } 
 

 
     if (aIsPrim || bIsPrim) { 
 
      return a === b; 
 
     } 
 

 
     if (a.prototype !== b.prototype) { 
 
      return false; 
 
     } 
 

 
     if (circ.a.indexOf(a) === -1) { 
 
      circ.a.push(a); 
 
     } else { 
 
      aCirc = true; 
 
     } 
 

 
     if (circ.b.indexOf(b) === -1) { 
 
      circ.b.push(b); 
 
     } else { 
 
      bCirc = true; 
 
     } 
 

 
     if (aCirc && bCirc) { 
 
      circ.cnt += 1; 
 
     } else { 
 
      circ.cnt = 0; 
 
     } 
 

 
     if (circ.cnt > 200) { 
 
      throw new RangeError('Circular reference limit exceeded'); 
 
     } 
 

 
     aIsArgs = $isArguments(a); 
 
     bIsArgs = $isArguments(b); 
 
     if ((aIsArgs && !bIsArgs) || (!aIsArgs && bIsArgs)) { 
 
      return false; 
 
     } 
 

 
     if (aIsArgs) { 
 
      return de(Array.prototype.slice.call(a), Array.prototype.slice.call(b), circ); 
 
     } 
 

 
     ka = Object.keys(a); 
 
     kb = Object.keys(b); 
 
     length = ka.length; 
 
     if (length !== kb.length) { 
 
      if (Array.isArray(a) && Array.isArray(b)) { 
 
       if (a.length !== b.length) { 
 
        return false; 
 
       } 
 
      } else { 
 
       return false; 
 
      } 
 
     } else { 
 
      ka.sort(); 
 
      kb.sort(); 
 
      for (index = 0; index < length; index += 1) { 
 
       if (ka[index] !== kb[index]) { 
 
        return false; 
 
       } 
 
      } 
 
     } 
 

 
     for (index = 0; index < length; index += 1) { 
 
      it = ka[index]; 
 
      if (!de($getItem(a, it), $getItem(b, it), circ)) { 
 
       return false; 
 
      } 
 
     } 
 

 
     aType = typeof a; 
 
     bType = typeof b; 
 

 
     return aType === bType; 
 
    }; 
 

 
    if (!Object.prototype.deepEqual) { 
 
     Object.defineProperty(Object.prototype, 'deepEqual', { 
 
      enumerable: false, 
 
      configurable: true, 
 
      writable: true, 
 
      value: function (b) { 
 
       var a = this; 
 

 
       return de(a, b, { 
 
        a: [], 
 
        b: [], 
 
        cnt: 0 
 
       }); 
 
      } 
 
     }); 
 
    } 
 

 
    if (!Array.prototype.unique) { 
 
     Object.defineProperty(Array.prototype, 'unique', { 
 
      enumerable: false, 
 
      configurable: true, 
 
      writable: true, 
 
      value: function (equalFn, thisArg) { 
 
       var object = Object(this), 
 
        length, 
 
        index, 
 
        eqFn, 
 
        arr, 
 
        idx, 
 
        val, 
 
        it; 
 

 
       if ($isUndefined(equalFn)) { 
 
        eqFn = $strictEqual; 
 
       } else { 
 
        eqFn = equalFn; 
 
       } 
 

 
       if (!$isFunction(eqFn)) { 
 
        throw new TypeError('Argument is not a function: ' + eqFn); 
 
       } 
 

 
       arr = []; 
 
       length = object.length >>> 0; 
 
       for (index = 0; index < length; index += 1) { 
 
        if (index in object) { 
 
         it = $getItem(object, index); 
 
         val = true; 
 
         for (idx = 0; idx < length; idx += 1) { 
 
          if (idx < index && idx in object && eqFn.call(thisArg, it, $getItem(object, idx))) { 
 
           val = false; 
 
           break; 
 
          } 
 
         } 
 

 
         if (val) { 
 
          arr.push(it); 
 
         } 
 
        } 
 
       } 
 

 
       return arr; 
 
      } 
 
     }); 
 
    } 
 
}()); 
 

 
var data1 = [1, 2, 2, 3, 3, 5, 5, 6, 6, 6], 
 
    data2 = [ 
 
     ['a', 'b', 'c'], 
 
     ['a', 'b', 'c'], 
 
     ['d', 'e', 'f'], 
 
     ['g', 'h', 'i'] 
 
    ], 
 
    equals = Function.prototype.call.bind(Object.prototype.deepEqual), 
 
    pre = document.getElementById('out'); 
 

 
pre.textContent = JSON.stringify(data1.unique(equals), null, 2); 
 
pre.textContent += '\n\n'; 
 
pre.textContent += JSON.stringify(data2.unique(equals), null, 2);
<pre id="out"></pre>

相关问题