当前位置:网站首页>JS daily development tips (continuous update)
JS daily development tips (continuous update)
2022-07-01 03:21:00 【Brave Xiao Chen】
Preface
Some problems in daily development are summarized JS Tips , It simplifies the code , Make the code look more concise .
One 、 Array weight removal
There are many methods of array de duplication , such as :for loop 、 double for Cycle, etc , The idea is to generate a new array , Then go through the original array , Judge whether the new array has the value of the original array , If there is one, just skip , Add... If not , But it's a lot of code to write , It's not simple enough , And it's too much trouble , Here is a simple array de duplication method .
var arr = [0,1,2,3,4,5,6,7,8,9,0,1,5,7,84,,5,6,8,4,7,4]
//new Set( ) Generate a class array object with element uniqueness
console.log(new Set(arr));//{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 84, undefined }
//... The expand operator expands the class array object into the array
console.log([...new Set(arr)]);//[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 84, undefined ]Two 、 Array intersection
Array intersection , It is also a common requirement , The general idea is to judge whether there is this value in the array , If not, skip , Take it out if you have one . The idea is right , But the code may be troublesome , Here is a simple method to get the intersection of arrays .
const arr1 = [1, 2, 3, 4]
const arr2 = [2, 3, 4, 5]
//filter The filter returns qualified values to generate a new array
//includes Array methods , Determine whether there is this value in the array , If so, return to true Otherwise return to false
let arr3 = arr1.filter(item => arr2.includes(item))
console.log(arr3);//[2,3,4]3、 ... and 、 Array Union
Generally, it can also be solved through circulation , But it's too much trouble , Now I share a simple method of array union .
const arr1 = [1, 2, 3, 4]
const arr2 = [2, 3, 4, 5]
//filter The filter returns qualified values to generate a new array
//includes Array methods , Determine whether there is this value in the array , If so, return to true Otherwise return to false
//concat Array methods , Used to merge arrays
let arr3 = arr1.concat(arr2.filter(item => !arr1.includes(item)))
console.log(arr3);//[2,3,4]Four 、 If there is one in the array, delete , Add if not
How to write by yourself , It can be used directly , But the feeling is not the simplest , Optimization under later research . You can recommend some simple methods .
const arr1=[1,2,3,4]
const processing = (num)=>{
if(arr1.includes(num)){
arr1.splice(arr1.indexOf(num),1)
}else{
arr1.push(num)
}
console.log(arr1);
}
processing(3)5、 ... and 、 use ?? Instead of || More rigorous
?? Also known as null Judgment operator , Only when the left is undefined or null when , Just take the value on the right , Otherwise, take the value on the left . And || The difference is || Words ,|| On the left for 0 and false when , Will take the value on the right .
const num = 0 || 1
console.log(num);//1
const num1 = 0 ?? 1
console.log(num1);//06、 ... and 、 use ?. replace && And the trinary operator
?. It is called a chained operator , Only when the left is null or undefined When , Will take the value on the right .
?. Judge directly in the chain call , Judge whether the object on the left is null or undefined, If yes, , It's not going to go down , return undefined, If not , Returns the value on the right
const obj={
name:' Brave Xiao Chen '
}
console.log(obj&&obj.name);// Brave Xiao Chen
console.log(obj?obj:obj.name);//{ name: ' Brave Xiao Chen ' }
console.log(obj?.obj.name);7、 ... and 、 simplify if Judge
complex if Judgment doesn't just look disgusting , It's even more disgusting for people who are obsessed with code cleanliness , I don't want to write so much if ah , But there's no way , In fact, there are ways , I would like to recommend simplification if The way to judge . Through the optimized method , Can be more concise and easy to understand , And it is easier to maintain later .
// Code common if Judge
let title=''
if(num===1){
title=' Completed '
}
if(num===2){
title=' Hang in the air '
}
if(num===3){
title=' Have in hand '
}
// Optimized code
let obj={
1:' Completed ',
2:' Hang in the air ',
3:' Have in hand '
}
const handle=(num)=>{
return obj[num]
}
console.log(handle(1));8、 ... and 、 Simplified by Sanmu if...else...
For some if...else The judgment of the , We can further simplify it through Miki
// Before simplification
let title=''
let identification=false
if(identification){
title=' Completed '
}else{
title=' Hang in the air '
}
// Simplify through the trinocular operator
title=identification?' Completed ':' Hang in the air '2022.06.30
边栏推荐
- How to verify whether the contents of two files are the same
- Introduction to EtherCAT
- 雪崩问题以及sentinel的使用
- E15 solution for cx5120 controlling Huichuan is620n servo error
- Keil5中如何做到 0 Error(s), 0 Warning(s).
- 几行事务代码,让我赔了16万
- [reading notes] copywriting realization -- four golden steps for writing effective copywriting
- 【小程序项目开发 -- 京东商城】uni-app 商品分类页面(下)
- 最好用的信任关系自动化脚本(shell)
- Network address translation (NAT) technology
猜你喜欢
性能测试常见面试题
![[us match preparation] complete introduction to word editing formula](/img/e4/5ef19d52cc4ece518e79bf10667ef4.jpg)
[us match preparation] complete introduction to word editing formula

MySQL knowledge points

How to verify whether the contents of two files are the same

Multithreaded printing

伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示

POI exports excel and displays hierarchically according to parent-child nodes
![[applet project development -- Jingdong Mall] classified navigation area of uni app](/img/cb/b0b79444dc90980cd2220ff9e68549.png)
[applet project development -- Jingdong Mall] classified navigation area of uni app

Chapter 03_ User and authority management
![Servlet [first introduction]](/img/2a/aff3b93e43550d30a33c1683210d3a.png)
Servlet [first introduction]
随机推荐
[small program project development -- Jingdong Mall] the home page commodity floor of uni app
Introduction to ieda right click source file menu
Multithreaded printing
JS日常开发小技巧(持续更新)
Nacos
最好用的信任关系自动化脚本(shell)
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
gcc使用、Makefile总结
Introduction and installation of Solr
力扣-两数之和
数组的includes( )
Redis高效点赞与取消功能
家居网购项目
Detailed explanation of pointer array and array pointer (comprehensive knowledge points)
Mybati SQL statement printing
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
File upload and download
POI导出excel,按照父子节点进行分级显示
数据交换 JSON
Stop saying that you can't solve the "cross domain" problem