SOURCE

//普通对象的去重
let a1 = {key1:"key1",key2:"key2",key2:"key2",key3:"key3",key3:"key3"};
let b1 = Object.assign({},a1)
console.log(JSON.stringify(b1));



//普通数组的去重

let a2 = [1,2,2,3,3,4,4,5];
let b2 = Array.from(new Set(a2));
console.log(b2)
let c2 = [...new Set(a2)];
console.log(c2)
//Array.from的.map式用法
let a3 = [
    {key:"1",name:"甲"},
    {key:"2",name:"乙"},
    {key:"3",name:"丙"},
    {key:"4",name:"丁"},
    ];

let b3 = Array.from(a3,({name})=>name);//简写
let c3 = Array.from(a3,(item)=>item.name);//展开
console.log(b3)
console.log(c3)

//快捷清空数组
a3.length = 0;

//快捷将数组转为类数组对象
a2 = {...a2};
console.log(JSON.stringify(a2));


//展开运算符可以替代concat拼接数组
let a4=[1,2];
let b4=[3,4];
console.log([...a4,...b4])

//求两个数组的交集--先去重再遍历其中一个,遍历时判断每个元素看是否存在于另一个
let a5 = [0,2,4,6,8,8];
let b5 = [1,2,3,4,5,6];
//
let c5 = [...new Set(a5)].filter((item)=>b5.includes(item));
console.log(c5);

//简易从数组中删除虚值
let a6 = [0,'blue','',NaN,9,true,undefined,false];
let b6 = a6.filter((item)=>item)
    //相当于(item)=>{if(Boolean(item))return true;else return false;}
console.log(b6)

//从数组中获取随机值--基于数组长度取随机值下标索引即可
let c6 = a5[(Math.floor(Math.random()*(a5.length)))];
console.log(c6)

//对数组所有值进行求和——结合reduce
let sum5 = a5.reduce((x,y)=>x+y);
console.log(sum5)
console 命令行工具 X clear

                    
>
console