js 常用

过滤

let  paid =res.data.data.filter(item =>item.status==1)

 排序
let paid = [{a:1,name:你好},{a:0,name:你好}]
  paid.sort((a,b)=>{
    //return( a[a] - b[a])
    return( a.a- b.a)
})

 




 

 

 

 

 

深拷贝

let cp = JSON.parse(JSON.stringify(obj));

结构赋值

let {aa, bb, dd, ee} = obj;
let newObj = {aa, bb, dd, ee};

forEach

let arr = [1,2,3];
arr.forEach(i => console.log(i))

forMap

let arr = [1,2,3,4,5];
let res = arr.map(i => i * i);

 截取

 var str=location.href; 
               var s=str.indexOf("?"); 
               var t=str.substring(s+1)
               var a=t.lastIndexOf("#");
              var stt=t.substring(0,a)
//以? 开始 #结束

数组求平均值

const average = (...nums) => nums.reduce((acc, val) => acc + val, 0) / nums.length;
average(...[1, 2, 3]); // 2
average(1, 2, 3); // 2

 

posted @ 2024-05-29 18:08  心动12138  阅读(4)  评论(0编辑  收藏  举报