释放双眼,带上耳机,听听看~!
此文章持续更新
数组对象去重
- 方法一
let arr = [{ id: 1, test: '哈哈' }, { id: 2, test: '呵呵' }, { id: 1, test: '哈哈' }, { id: 3, test: '哈哈' }] var obj = {}; arr = arr.reduce((item, next) => { obj[next.id] ? '' : obj[next.id] = true && item.push(next); return item; }, []); console.log(arr);
- 方法二
function unique(arr, key) { return arr.filter((item, index, arr) => { return arr.findIndex(row => { return row[key] == item[key] }) == index }) }
日期分割
- 方法一
function insertStr(soure, firstPosition, firstStr, secondPosition, secondStr) { soure = soure.slice(0, firstPosition) + firstStr + soure.slice(firstPosition) return soure.slice(0, secondPosition) + secondStr + soure.slice(secondPosition) } let currentTime = "20210323" console.log(insertStr(currentTime, 4, '-', 7, '-'))//2021-03-23
内容投诉