记一次实习收获

随记 0 654 0
发表于: 2020-04-22 21:42:59

简介: 暂无~

单词倒序

function desc(str) {
    var arr = str.split(" ")
    var res = []
    for (var i = 0; i < arr.length; i++) {
        res.unshift(arr[i])
    }
    return res.join(" ")
}

var str = 'hello word i come here'
console.log(desc(str))

去重

        function distinct1(arr, key) {
            var newobj = {}, newArr = [];
            for (var i = 0; i < arr.length; i++) {
                var item = arr[i];
                if (!newobj[item[key]]) {
                    newobj[item[key]] = newArr.push(item);
                }
            }
            console.log(newArr)
            return newArr;
        }
        var arr1 = [
            { name: '张三' },
            { name: '张三' },
            { name: '李四' },
            { name: '王五' },
        ]
        distinct1(arr1, 'name')

分组

        var arr1 = [
            { "id": "1001", "name": "值1" },
            { "id": "1001", "name": "值1" },
            { "id": "1002", "name": "值2" },
            { "id": "1002", "name": "值2" },
            { "id": "1002", "name": "值2" },
            { "id": "1003", "name": "值3" }
        ];

        function groupby(arr, groups) {
            var map = {},
                dest = [];
            for (var i = 0; i < arr.length; i++) {
                var ai = arr[i];
                if (!map[ai.id]) {
                    dest.push({
                        id: ai.id,
                        groups: [ai]
                    });
                    map[ai.id] = ai;
                } else {
                    for (var j = 0; j < dest.length; j++) {
                        var dj = dest[j];
                        if (dj.id == ai.id) {
                            dj.groups.push(ai);
                            break;
                        }
                    }
                }
            }
            return dest
        }
        console.log(arr1)
        console.log(groupby(arr1, 'id'));

深拷贝

        deepClone = (initalObj) => {
            const obj = {};
            if (typeof initalObj !== 'object') {
                return initalObj
            }
            for (const key in initalObj) {
                if (typeof initalObj[key] === 'object') {
                    //对数组特殊处理
                    if (Array.isArray(initalObj[key])) {
                        //用map方法返回新数组,将数组中的元素递归
                        obj[key] = initalObj[key].map(item => this.deepClone(item))
                    } else {
                        //递归返回新的对象
                        obj[key] = this.deepClone(initalObj[key]);
                    }
                } else if (typeof initalObj[key] === 'function') {
                    //返回新函数
                    obj[key] = initalObj[key].bind(obj);
                } else {
                    //基本类型直接返回
                    obj[key] = initalObj[key];
                }
            }
            return obj;
        }
        const obj = {
            a: 1,
            b: {},
            c: { d: {}, g: () => { } },
            e: () => { },
            f: function () { }
        }
        const newObj = deepClone(obj);
实习记录

最后更新于:2021-02-16 21:07:10

欢迎评论留言~
0/400
支持markdown
Comments | 0 条留言
按时间
按热度
目前还没有人留言~