ハッシュ表を用いて複雑な配列を再構成する.

4357 ワード

    var arrNew = [{'num':1},[1,2,3],{'num':1},{'num':'1'},'  ',[1,2,3],[2,3,4,5],1,'  ',2,3,'hehe',[2,3,4,5],'hehe','  '];

//            
    function dedup(arr) {
        let hashTable = {};
        return  arr.filter(el =>{
            let key = JSON.stringify(el);
            let match = Boolean(hashTable[key]);
            return (match ? false : hashTable[key] = true);
        })
    }
    console.log(dedup(arrNew)); //[ { num: 1 },[ 1, 2, 3 ],{ num: '1' },'  ',[ 2, 3, 4, 5 ],1,2,3,'hehe','  ' ]