This is the second day of my participation in the August Text Challenge.More challenges in August
Js array Notes
Implements the summation of identical elements in an object
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 = {}; / / move | telecom
const total_mobile_unicom = {}; / / move | unicom
const total_unicom_chinanet = {}; / / unicom | telecom
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
Copy the code
1. Js iterates through the array and adds new attributes to each object element
let arry = this.teamList
let arryNew = []
arry.map((item, index) = > {
arryNew.push(Object.assign({}, item, {
disabled: true}})))Copy the code
2. Js delete an item from array splice()
var id = '3'
var tagList = [{
"parentTagId": "1"."parentTagName": "School"."childTagId": "3"."childTagName": "High school"
},
{
"parentTagId": "1"."parentTagName": "School"."childTagId": "4"."childTagName": "Junior"}]for (var i = 0; i < tagList.length; i++) {
if (tagList[i].childTagId === id) {
tagList.splice(i, 1); }}console.log(JSON.stringify(tagList))
Copy the code
3. Js randomly generates a four-digit verification code (including letters and numbers)
function checkCodeofRandom() {
// An array of randomly selected samples is required
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"."Seven"."8"."9");
// Initializes the concatenation string
var str = "";
for (i = 0; i < 4; i++) {
// Generate a number between 0 and 61 each time as a random subscript to obtain the captcha
var p = Math.floor(Math.random() * 1000) % 62;
// Splicing the verification code randomly selects uppercase and lowercase letters and numbers
str += nums[p];
}
console.log(str)
return str;
}
checkCodeofRandom()
// No server verification is required
// The client checks whether the verification code entered by the user is consistent with the randomly generated verification code
Copy the code
3. Object splicing.
Put the data of two objects in one object
Methods a
var tmpa = {
q: 'qq'.w: 'ww'
}
var tmpb = {
e: 'ee'.r: 'rr'
}
var tmp = Object.assign(tmpa, tmpb)
console.log("tmp", tmp)
Copy the code
Method 2
var tmpa = {
q: 'qq'.w: 'ww'
}
vartmpb = { ... tmpa,e: 'ee'.r: 'rr'
}
console.log("tmp", tmpb)
Copy the code
4. A deep copy
function deepCopy(obj) {
if (typeofobj ! = ='object') {
return
}
const str = JSON.stringify(obj)
return JSON.parse(str)
}
export default deepCopy
Copy the code
5. Picture placeholder hidden
img:not([src]):not([srcset]) {
visibility: hidden;
}
Copy the code
6. Replace the letters after the specified string
var abc = 'adadada=ss';
var j = abc.substring(abc.indexOf('=') + 1, abc.length);
var dsd = abc.replace(j, 'haha');
dsd = 'adadada=haha'
Copy the code
7. How do I set the value of val to be null for traversing multiple objects
let obj = {
"a": 1."b": 2
};
Object.keys(obj).map(key= > obj[key] = ' ')
console.log(obj)
Copy the code
8. Array deduplication
function unique(arr) {
return arr.filter(function(item, index, arr) {
// Current element, the first index in the original array == current index value, otherwise return current element
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",{},{}]
Copy the code