比较变态的数组去重,求方法!!!

var a = ['hello',{hello: '323651',bye: '43434'},[1,2,34],true,10,9,8,10,'true','hello',true,false,9,{hello: '312312',ok: 32323},[1,2,34]];

慕尼黑5688855
浏览 459回答 1
1回答

哆啦的时光机

数字,布尔型等直接在后面加判断就好,加在一起也行,我这么写你看着更明白。var a = ['{"1":1,"2":2}', {1:1, 2:2}, 'hello',{hello: '323651',bye: '43434'},[1,2,34],true,10,9,8,10,'true','hello',true,false,9,'9',{hello: '312312',ok: 32323},[1,2,34]];var json = {};var arr = [];for (let i = 0, len = a.length; i < len; i++) {&nbsp; let str = JSON.stringify(a[i]) + typeof a[i];&nbsp; if (!json[str]) {&nbsp; &nbsp; arr.push(a[i]);&nbsp; }&nbsp; json[str] = 1;}console.log(arr);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript