1、复杂数组去重
let arr = [ { name: 'atom',age: '15'}, { name: 'aili',age:'16'}, { name:'atom',age:'18'}, { name: 'aili', age: '16'} ] function removeRepeatArray(arr) { let unique = {}; arr.forEach(key => { unique[JSON.stringify(key)] = key; }); arr = Object.keys(unique).map(data => JSON.parse(data)); return arr; } console.log(removeRepeatArray(arr)) // [ // { name: 'atom',age: '15'}, // { name: 'aili',age:'16'}, // { name:'atom',age:'18'}, //]
2、1+2+3....+100之和
let i=0,item,n=100,sum; sum = (1+ n) *n / 2; console.log(sum) //5050
3、数组重复个数
let arr = ['apple','orange', 'pear', 'apple', 'orange'] function getWordCnt () { return arr.reduce(function(prev, next){ prev[next] = (prev[next] + 1) || 1 return prev; },{}) } console.log(getWordCnt()) //{apple: 2, orange: 2, pear: 1}
4、数据类型检测
function classOf(obj) { if (obj === null) return "Null"; if (obj === undefined) return 'Undefined'; return Object.prototype.toString.call(obj).slice(8, -1); }