You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ykshao, dora1988, jsGood, zhangyanling77, Alan110 and 127 moremonacekairoy and kakatigerClubJ and kakatigerkeeperererer, Xu-Angel, Realzzz1874, Wal1e, yishuihan-001 and 3 more
// 1、扁平化数组
var flatArr = arr.toString().split(",");
// 2、去重
var hash = {};
for (var i = 0, len = flatArr.length; i < len; i++) {
hash[flatArr[i]] = "abc"
}
flatArr = [];
// 3、将元素字符串转化为数字、遍历hash并不能保证输出顺序
for (var i in hash) {
flatArr.push(+i)
}
// 4、排序
flatArr = flatArr.sort(function(a, b) {
return a - b
})
console.log(flatArr)
OldSixLi, huwenkang99, lunhui1994, MblackAngel, MarsZone and 1 more
Activity
zpzxgcr commentedon Feb 12, 2019
zpzxgcr commentedon Feb 12, 2019
相关知识点
es6数组常用操作
hejiaqian commentedon Feb 12, 2019
hejiaqian commentedon Feb 12, 2019
zpzxgcr commentedon Feb 12, 2019
你可能后面需要
arr.toString().split(",").sort((a,b)=>{ return a-b}).map(Number)
不然数组元素都是字符串 结果并没有去重 还有两个2 5和12
hejiaqian commentedon Feb 12, 2019
谢谢大佬指点,学习了
[-]将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组[/-][+]第11题:将数组扁平化并去除其中重复数据,最终得到一个升序且不重复的数组[/+]cjfff commentedon Feb 12, 2019
AprilTong commentedon Feb 12, 2019
zpzxgcr commentedon Feb 12, 2019
函数名应该是flat
cjfff commentedon Feb 12, 2019
w587 commentedon Feb 12, 2019
jackchenl commentedon Feb 13, 2019
递归函数解法:
zhuzhuoxingguang commentedon Feb 14, 2019
利用前面的答案,修正一个没用ES6的写法:
sisterAn commentedon Feb 16, 2019
看一道面试题:
答案:
关于 Set 请查阅 Set、WeakSet、Map及WeakMap
本文首发于我的博客:数组扁平化、去重、排序
398 remaining items