本文介绍了code可与数组,但不能对多维数组。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

工作的一个扑克手评估,以及牦牛剃须的部分是写怎样的5连击很多你想从7张牌获得功能(pickNofSet())。我这样做,但我的方式做了一个返回一堆重复的。

所以,我必须写一个removeDuplicates()。这里的问题......它与一个简单的数组,但它不与数组的数组的工作,我的pickNofSet函数生成。

- 这里的removeDuplicates code -

  VAR removeDuplicates =功能(输入){//采用数组
VAR输出= [];
对于(i = 0; I< input.length;我++){
    VAR独特= TRUE; //所有的元素都是无辜的,直到被证明有罪
    为(J = I + 1; J< input.length; J ++){
        如果(输入[J] ===输入[I]){
            独特= FALSE; // 有罪!
        };// 万一
    }; //结束人权保护中心
    如果(唯一){//如果没有被判有罪,
        output.push(输入[I]); //你可以自由自在地去,小元
    };// 万一
}; //最终IFOR
的console.log(输出);
返回输出; }; //函数结束

下面是我从控制台得到:

 > removeDuplicates(['一','B','C'],['一','B','C'],['D','E','F'],['G',' H','我']);
<未定义
> removeDuplicates([1,2,2,3,3,5,5,6,6,6]);
< [1,2,3,5,6]


解决方案

下面是一个通用的独特的过滤器,应填写您需要的例子。需要ES5兼容的环境。

\r
\r

(函数(){\r
    使用严格的;\r
\r
    功能$ strictEqual(A,B){\r
        返回=== B:\r
    }\r
\r
    功能$ isUndefined(inputArg){\r
        返回$ strictEqual(typeof运算inputArg,未定义);\r
    }\r
\r
    功能$ isPrimitive(inputArg){\r
        VAR类型= ty​​peof运算inputArg;\r
\r
        返回类型==='不确定'|| inputArg ===空||类型==='布尔'||键入==='串'||类型==='号'||键入==='符号';\r
    }\r
\r
    功能$ isFunction(inputArg){\r
        返回的typeof inputArg ===功能;\r
    }\r
\r
    功能$而isDate(inputArg){\r
        返回Object.prototype.toString.call(inputArg)=== [对象日期]';\r
    }\r
\r
    功能$ isRegExp(inputArg){\r
        返回Object.prototype.toString.call(inputArg)=== [对象正则表达式]';\r
    }\r
\r
    功能$ isString(inputArg){\r
        返回Object.prototype.toString.call(inputArg)=== [对象字符串]';\r
    }\r
\r
    功能$ isArguments(inputArg){\r
        返回Object.prototype.toString.call(inputArg)=== [对象参数]';\r
    }\r
\r
    功能$的getItem(对象,索引){\r
        VAR项目;\r
\r
        如果($ isString(对象)){\r
            项目= object.charAt(指数);\r
        }其他{\r
            项目=对象[指数]\r
        }\r
\r
        归还物品;\r
    }\r
\r
    VAR德=功能(A,B,保监会){\r
        如果(一个=== B){\r
            返回true;\r
        }\r
\r
        VAR atype的,\r
            BTYPE,\r
            aIsArgs,\r
            bIsArgs,\r
            aIsPrim,\r
            bIsPrim,\r
            ACIRC,\r
            bCirc,\r
            KA,\r
            KB,\r
            长度,\r
            指数,\r
            它;\r
\r
        如果($而isDate(一)及和放大器; $而isDate(B)){\r
            返回a.getTime()=== b.getTime();\r
        }\r
\r
        如果($ isRegExp(一)及和放大器; $ isRegExp(B)){\r
            返回a.source === b.source&放大器;&安培;\r
                a.global === b.global&放大器;&安培;\r
                a.multiline === b.multiline&放大器;&安培;\r
                a.lastIndex === b.lastIndex&放大器;&安培;\r
                a.ignoreCase === b.ignoreCase&放大器;&安培;\r
                a.sticky === b.sticky;\r
        }\r
\r
        aIsPrim = $ isPrimitive(一);\r
        bIsPrim = $ isPrimitive(二);\r
        如果((aIsPrim || $ isFunction(一))及及(bIsPrim || $ isFunction(二))){\r
            / * JSLint的eqeq:真* /\r
            返回== B:\r
        }\r
\r
        如果(aIsPrim || bIsPrim){\r
            返回=== B:\r
        }\r
\r
        如果(a.prototype!== b.prototype){\r
            返回false;\r
        }\r
\r
        如果(circ.a.indexOf(一)=== -1){\r
            circ.a.push(一);\r
        }其他{\r
            ACIRC =真;\r
        }\r
\r
        如果(circ.b.indexOf(二)=== -1){\r
            circ.b.push(二);\r
        }其他{\r
            bCirc = TRUE;\r
        }\r
\r
        如果(ACIRC&安培;&安培; bCirc){\r
            circ.cnt + = 1;\r
        }其他{\r
            circ.cnt = 0;\r
        }\r
\r
        如果(circ.cnt> 200){\r
            抛出新的RangeError('循环引用超限');\r
        }\r
\r
        aIsArgs = $ isArguments(一);\r
        bIsArgs = $ isArguments(二);\r
        如果((aIsArgs&安培;&安培;!bIsArgs)||(aIsArgs&安培;!&安培; bIsArgs)){\r
            返回false;\r
        }\r
\r
        如果(aIsArgs){\r
            返回德(Array.prototype.slice.call(一),Array.prototype.slice.call(b)中,保监会);\r
        }\r
\r
        か= Object.keys(一);\r
        KB = Object.keys(B);\r
        长度= ka.length;\r
        如果(长度!== kb.length){\r
            如果(Array.isArray(a)及;&放大器; Array.isArray(b))的{\r
                如果(则为a.length!== b.length个){\r
                    返回false;\r
                }\r
            }其他{\r
                返回false;\r
            }\r
        }其他{\r
            ka.sort();\r
            kb.sort();\r
            对于(指数= 0;指数 - LT;长度指数+ = 1){\r
                如果(KA [指数] == KB [指数]){\r
                    返回false;\r
                }\r
            }\r
        }\r
\r
        对于(指数= 0;指数 - LT;长度指数+ = 1){\r
            它=嘉[指数]\r
            如果(!德($的getItem(一,吧),$的getItem(B,吧),中国保监会)){\r
                返回false;\r
            }\r
        }\r
\r
        ATYPE = typeof运算一个;\r
        BTYPE = typeof运算B:\r
\r
        返回ATYPE === BTYPE;\r
    };\r
\r
    如果(!Object.prototype.deepEqual){\r
        Object.defineProperty(Object.prototype中,'deepEqual',{\r
            枚举:假的,\r
            配置:真实,\r
            可写的:真实,\r
            价值:功能(B){\r
                VAR一个=这一点;\r
\r
                返回去(A,B,{\r
                    一个: [],\r
                    乙:[],\r
                    CNT:0\r
                });\r
            }\r
        });\r
    }\r
\r
    如果(!Array.prototype.unique){\r
        Object.defineProperty(Array.prototype,独一无二,{\r
            枚举:假的,\r
            配置:真实,\r
            可写的:真实,\r
            价值:功能(equalFn,thisArg){\r
                VAR对象=对象(本),\r
                    长度,\r
                    指数,\r
                    eqFn,\r
                    ARR,\r
                    IDX,\r
                    VAL,\r
                    它;\r
\r
                如果($ isUndefined(equalFn)){\r
                    eqFn = $ strictEqual;\r
                }其他{\r
                    eqFn = equalFn;\r
                }\r
\r
                如果(!$ isFunction(eqFn)){\r
                    抛出新的TypeError('参数不是一个函数:'+ eqFn);\r
                }\r
\r
                ARR = [];\r
                长度= object.length>>> 0;\r
                对于(指数= 0;指数 - LT;长度指数+ = 1){\r
                    如果(对象索引){\r
                        它= $的getItem(对象,索引);\r
                        VAL = TRUE;\r
                        对于(IDX = 0; IDX<长度IDX + = 1){\r
                            如果(IDX<&指数放大器;&功放;在对象和放大器IDX;&安培; eqFn.call(thisArg,它,$的getItem(对象,IDX))){\r
                                VAL = FALSE;\r
                                打破;\r
                            }\r
                        }\r
\r
                        如果(VAL){\r
                            arr.push(它);\r
                        }\r
                    }\r
                }\r
\r
                返回ARR;\r
            }\r
        });\r
    }\r
}());\r
\r
变种DATA1 = [1,2,2,3,3,5,5,6,6,6],\r
    数据2 = [\r
        ['一','B','C'],\r
        ['一','B','C'],\r
        ['D','E','F'],\r
        ['G','H','我']\r
    ]\r
    等于= Function.prototype.call.bind(Object.prototype.deepEqual)\r
    pre =的document.getElementById('出');\r
\r
pre.textContent = JSON.stringify(data1.unique(等于),NULL,2);\r
pre.textContent + ='\\ n \\ n;\r
pre.textContent + = JSON.stringify(data2.unique(等于),NULL,2);

\r

< pre ID =走出去>< / pre>

\r

\r
\r

Working on a Holdem hand evaluator, and part of the yak shaving is writing a "how many combos of 5 do you get from 7 cards" function (pickNofSet()). I've done that, but the way I've done that returns a bunch of duplicates.

So I have to write a removeDuplicates(). Here's the problem... it works with a simple array, but it doesn't work with the "arrays of arrays" that my "pickNofSet" function generates.

-- here's the removeDuplicates code --

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

Here's what I get from the Console:

> 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]
解决方案

Here is an example of a general purpose unique filter that should fill your need. Requires an ES5 compliant environment.

(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>

这篇关于code可与数组,但不能对多维数组。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 07:56