Js各种小技巧总结

(一)深度拷贝数组

方案1:
let arr=[1,2,3] let arr1=[...arr]
方案2:
let arr=[1,2,3] let arr1 = JSON.parse(JSON.stringify(arr))

(二)解构赋值

数组:
let arr=['hello','world','hi','trouble'] let [a,b]=arr //a='hello' //b='world'
对象:
let obj={
  data:{
    data:{
      title:
"标题1",       name:"测试1"
    }   }
} let {data:res}
=obj //res=data: {title: "标题1", name: "测试1"}

(三)模板字面量

在字符串中拼接表达式时就很方便:${变量}

模板字面量是用反引号(``)包围的字符串

axios({
  methods:
'GET',
  url: `${
this.url}/v1/${this.id}`
}).then(res
=> {   
  //业务代码
})
原文地址:https://www.cnblogs.com/zhaoyingzhen/p/15206241.html