JS的某些常用方法合集

释放双眼,带上耳机,听听看~!

此文章持续更新

数组对象去重

  1. 方法一
    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);​
  2. 方法二
    function unique(arr, key) {
        return arr.filter((item, index, arr) => {
            return arr.findIndex(row => {
                return row[key] == item[key]
            }) == index
        })
    }​

日期分割

  1. 方法一
        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​
内容投诉
JavaScript

HTML5进阶FileReader的使用方法

2021-1-12 16:01:14

JavaScript

canvas通过js给网页添加水印

2021-6-7 17:28:06

搜索