口试必问的数组去反复

假如你如今正在口试请坚持岑寂的思想,从容应对;由于思想一热能够该会的也不会了,下面贴出来一段简朴的数组去反复的要领,写的有点简朴仅供人人参考,假如不足之处请多见教;

var ary = [1,2,3,2,1,4,4,423,323,"1","323"];
    function getSingleArray(ary){
        var obj = {},
            newArray = [];
        if(typeof ary === "object"){
            for(var i=0; i<ary.length; i++){
                var cur = ary[i];
                if(!!obj[cur] && typeof obj[cur]==typeof cur){
                    continue;
                }else{
                    obj[cur] = cur;
                    newArray.push(obj[cur]);
                }
            }
        }
        return newArray;
    }
    var b = getSingleArray(ary);
    console.log("新数组:",b);
    console.log(ary);

只是简朴的推断,后续会愈来愈完美推断数组的范例

    原文作者:D一生
    原文地址: https://segmentfault.com/a/1190000004947844
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞