寫一個函數實現js數組任意類型去重:輸入:["a","a",0, 0, {}, {}, {a:1},{a:1},[],[],[1],[1],null, null, undefined,undefined, /\.js$/, /\.js$/]輸出:["a", 0 , {}, {a:1}, [], [1], null, undefined, /\.js$/]
2 回答

慕桂英3389331
TA貢獻2036條經驗 獲得超8個贊
const unique = (array) => { let obj = {} return array.filter((item, index) => { // 防止key重復 let newItem = item + JSON.stringify(item) return obj.hasOwnProperty(newItem) ? false : obj[newItem] = true }) }
添加回答
舉報
0/150
提交
取消