笔记

98 阅读1分钟

1.删除数组里undefined元素
blog.csdn.net/qq_33769914…

2.深拷贝

deepClone(obj={}){
        if(typeof obj!='object'||obj==null){
          return obj
        }
        let result;
        if(obj instanceof Array){
          result=[];
        }else{
          result={};
        }
        for(let key in obj){
          if(obj.hasOwnProperty(key)){
            result[key]=this.deepClone(obj[key]);
          }
        }
        return result;
      },

3.div设置contenteditable=“true”时可编辑 event.target.innerText = val;(动态赋值)

4.Vue 简单的记录div滚动条的位置,并返回顶部
在最外层的div:<div @scroll="divScroll" >

引入返回顶部的图标:

<div @click="scrollTop"></div>

 divScroll(div) {
this.scroll = div; 
}
 scrollTop() {
this.scroll.target.scrollTop = 0;
}

5.Js根据数组相同的值生成二维数组

    var list = [
        { id: 1, math: 3, },
        { id: 1, math: 3, },
        { id: 1, math: 2, },
        { id: 2, math: 1, },
        { id: 2, math: 2, },
        { id: 3, math: 2, },
        { id: 3, math: 3, },
    ]

    var result = [];
    list.forEach((item, index) => {
        var { id } = item;
        if (!result[id]) {
            result[id] = {
                id,
                value: []
            }
        }
        result[id].value.push({ num: item.num });
    });
    var data = Object.values(result);
</script>

6.JavaScript - 二维数组(对象数组)中获取某个值在数组首次出现的位置下标

const arr = [ {a: 1}, {b: 2}, {c: 3} ]

// 获取属性【b = 2】数组项首次出现的位置(下标)
const result = arr.map(o => o.b).indexOf(2)

// print
console.log(result) //1

  1. js计算数组中重复元素的个数(包括对象数组)
    www.jianshu.com/p/23d4f03fb…