//reduce数组去重
let arr = [1,2,3,4,5,2,3]
let res=arr.reduce((pre,cur)=>{
if(!pre.includes(cur)){
pre.push(cur)
}
return pre
},[])
console.log(res)
//reduce数组去重
let newArr = Array.from(new Set(arr));
console.log(newArr);
//统计数组元素出现次数
let result = arr.reduce((pre,cur)=>{
if(cur in pre){
pre[cur]++
}else{
pre[cur] = 1
}
return pre
},{})
console.log(result)
//将二维数组转化为一维
let arr1 = [[0, 1], [2, 3], [4, 5]]
let newArr1 = arr1.reduce((pre,cur)=>{
return pre.concat(cur)
},[])
console.log(newArr1);
//将多维数组转化为一维
let arr2 = [[0, 1], [2, 3], [4,[5,6,7]]]
const newArr2 = function(arr2){
return arr2.reduce((pre,cur)=>pre.concat(Array.isArray(cur) ? newArr2(cur):cur),[])
}
console.log(newArr2(arr2));
//sum(1,2,3)=>sum(1)(2,3)
let sum =function (...args){
let sum1=args.reduce((pre,cur) => pre+cur)
let tol=(...args1) => {
sum1 += args1.reduce((pre,cur) => pre+cur)
return tol
}
tol.valueof = () =>sum1
return tol
}
console.log(sum(1,2,3).valueof())
console.log(sum(1)(2,3).valueof())
//url参数序列化
const getQueryString = function (urlstr) {
let res={};
urlstr = urlstr.substr(urlstr.indexOf('?')+1);
let args=urlstr.split("&");
for (let i=0 ;i<args.length;i++) {
let arg =args[i];
let item = arg.split("=");
res[item[0]]=item[1]
}
return res
}
var url='https://www.baidu.com/s?ie=utf-8&f=3&rsv_bp=1&rsv_idx=1&tn=baidu&wd=udacity%E5%AE%98%E7%BD%91&oq=nba'
console.log(getQueryString(url))