js数组笔记

223 阅读1分钟

这是我参与8月更文挑战的第2天,活动详情查看:8月更文挑战

js数组笔记

· 实现对象中各相同元素求和

    const obj = {
        "total_mobile": {
            "user_cnt_connect": 1,
            "user_cnt_fail": 1,
            "cnt_fail": 1,
            "fail_user_ratio": 1.87,
            "cnt_fail_per_user": 1,
            "fail_ratio": 1
        },
        "total_unicom": {
            "user_cnt_connect": 2,
            "user_cnt_fail": 2,
            "cnt_fail": 2,
            "fail_user_ratio": 2,
            "cnt_fail_per_user": 2,
            "fail_ratio": 2.28
        },
        "total_chinanet": {
            "user_cnt_connect": 3,
            "user_cnt_fail": 1.89,
            "cnt_fail": 3,
            "fail_user_ratio": 1,
            "cnt_fail_per_user": 1,
            "fail_ratio": 1
        }
    }

    const {
        total_mobile,
        total_chinanet,
        total_unicom
    } = obj;
    const total_mobile_chinanet = {}; //移动|电信
    const total_mobile_unicom = {}; //移动|联通
    const total_unicom_chinanet = {}; //联通|电信
    for (let k in total_mobile) {
        total_mobile_chinanet[k] = total_mobile[k] + total_chinanet[k];
        total_mobile_unicom[k] = total_mobile[k] + total_unicom[k];
        total_unicom_chinanet[k] = total_chinanet[k] + total_unicom[k];
    }
    console.log(total_mobile_chinanet)
    console.log(total_mobile_unicom)
    console.log(total_unicom_chinanet)
    var num = 2.446242342;
    num = num.toFixed(2);
    console.log(num); //2.45
    console.log(typeof num); // string

1.js遍历数组,并向每一个对象元素添加新属性

 let arry = this.teamList
 let arryNew = []
 arry.map((item, index) => {
     arryNew.push(Object.assign({}, item, {
         disabled: true
     }))
 })

2.js删除数组中某一项 splice()

var id = '3'
var tagList = [{
        "parentTagId": "1",
        "parentTagName": "学校",
        "childTagId": "3",
        "childTagName": "高中"
    },
    {
        "parentTagId": "1",
        "parentTagName": "学校",
        "childTagId": "4",
        "childTagName": "初中"
    }
]
for (var i = 0; i < tagList.length; i++) {
    if (tagList[i].childTagId === id) {
        tagList.splice(i, 1);
    }
}
console.log(JSON.stringify(tagList))

3.js 随机生成四位验证码(包括字母和数字)

 function checkCodeofRandom() {
     // 所需随机抽取的样本数组 
     var nums = new Array("q", "w", "e", "r", "t", "y", "u", "i", "o", "p", "a", "s", "d", "f", "g", "h", "j", "k", "l", "z", "x", "c", "v", "b", "n", "m", "A", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
     // 初始化 拼接字符串
     var str = "";
     for (i = 0; i < 4; i++) {
         //每次生成一个0 - 61 之间的 number 作为随机获取验证码的下标
         var p = Math.floor(Math.random() * 1000) % 62;
         //拼接验证码  随机抽取大小写字母和数字
         str += nums[p];
     }
     console.log(str)
     return str;
 }
 checkCodeofRandom()

 //不需要服务端做出校验
 //客户端根据用户输入的验证码跟随机生成的验证码是否一致来进行判断

3. 对象拼接。

说明:把两个对象的数据放在一个对象里面

方法一

var tmpa = {
    q: 'qq',
    w: 'ww'
}
var tmpb = {
    e: 'ee',
    r: 'rr'
}
var tmp = Object.assign(tmpa, tmpb)
console.log("tmp", tmp)

方法二

var tmpa = {
    q: 'qq',
    w: 'ww'
}
var tmpb = {
    ...tmpa,
    e: 'ee',
    r: 'rr'
}
console.log("tmp", tmpb)

4. 深拷贝

function deepCopy(obj) {
    if (typeof obj !== 'object') {
        return
    }
    const str = JSON.stringify(obj)
    return JSON.parse(str)
}

export default deepCopy

5. 图片占位隐藏

img:not([src]):not([srcset]) {
    visibility: hidden;
}

6. 替换指定字符串后的字母

var abc = 'adadada=ss';
var j = abc.substring(abc.indexOf('=') + 1, abc.length);
var dsd = abc.replace(j, 'haha');
dsd = 'adadada=haha'

7. 如何设置遍历多个对象,让他们的val值为空

let obj = {
    "a": 1,
    "b": 2
};
Object.keys(obj).map(key => obj[key] = '')
console.log(obj)

8.数组去重

 function unique(arr) {
     return arr.filter(function(item, index, arr) {
         //当前元素,在原始数组中的第一个索引==当前索引值,否则返回当前元素
         return arr.indexOf(item, 0) === index;
     });
 }
 var arr = [1, 1, 'true', 'true', true, true, 15, 15, false, false, undefined, undefined, null, null, NaN, NaN, 'NaN', 0, 0, 'a', 'a', {}, {}];
 console.log(unique(arr)) //[1,"true",true,15,false,null,null,"NaN",0,"a",{},{}]