查数组内重复的元素有几个
数组扁平化是指将一个多维数组变为一维数组[1, [2, 3, [4, 5]]] ------> [1, 2, 3, 4, 5]
<script>
//数组查重复的元素有几个
let str ='asasdxsadfsdgsdafdsaadfgasdfghasfssafasdfgasdfgh'
function sum(string) {
let obj = {}
for (let k in string) {
/* if (obj[string[k]]) {
obj[string[k]]++
} else {
obj[string[k]] = 1
} */
obj[string[k]] = string.split(string[k]).length -1
}
return obj
}
console.log(sum(str));
//数组扁平化
//多维数组转一维数组
let arr = [1,2,3,[4,5,[6,7,[8,9]]]]
function fn(arr) {
/* let newArr = []
for (var i in arr) {
if (Array.isArray(arr[i])) {
newArr = newArr.concat(fn(arr[i]))
} else {
newArr.push(arr[i])
}
}
return newArr */
//flat方法后面跟一个参数,参数是几,就展开几层,如果需要全部展开可以跟Infinity无限展开
return arr.flat(Infinity)
}
console.log(fn(arr));
</script>
//数组查重复的元素有几个letstr ='asasdxsadfsdgsdafdsaadfgasdfghasfssafasdfgasdfgh'functionsum(string) {letobj = {}for(letkinstring) {/* if (obj[string[k]]) {