當(dāng)前位置:首頁 > IT技術(shù) > Web編程 > 正文

js常用的方法
2021-09-23 06:50:06

1. 獲取 url 的參數(shù)

function getParameter(parName){

  var str = parName.toLowerCase() + "=";

  var gvalue = "";
  var HREF = location.href;
  var upperHREF = location.href.toLowerCase();
  if(upperHREF.indexOf(str)>0){
    gvalue = HREF.substring(upperHREF.indexOf(str) + str.length,upperHREF.length);
    if(gvalue.indexOf('&')>0) gvalue = gvalue.substring(0,gvalue.indexOf('&'));
      if(gvalue.indexOf("#")>0) gvalue = gvalue.split("#")[0];
    }  
    return gvalue;
}

?

2. 數(shù)組對(duì)象 根據(jù)條件過濾

  (1). 獲取年齡 18 的數(shù)據(jù)

    let array = [{id:1,name:"靚仔",age:18},{id:2,name:"靚女",age:18},{id:3,name:"瘦子",age:16},{id:4,name:"肥仔",age:28}]

?    function filterAge (array=[]){

? ? ? ?     // 年齡 18
? ? ? ?     return array.filter(ele=> ele.age == 18)
? ? ?    }
? ? ?    let? prams = filterAge(array)? //?[{id:1,name:"靚仔",age:18},{id:2,name:"靚女",age:18}]
  (2). 獲取數(shù)組對(duì)象的 id 組件一個(gè)數(shù)組

    let array = [{id:1,name:"靚仔",age:18},{id:2,name:"靚女",age:18},{id:3,name:"瘦子",age:16},{id:4,name:"肥仔",age:28}]

?    function filterId (array=[]){

? ? ? ??    // 獲取 id 組成數(shù)組
? ? ? ??    return array.filter(ele=> ele.id)
? ? ??   }
? ? ??   let? prams =?filterAge(array)? //?[1,2,3,4]
  (3). 給數(shù)組對(duì)象中的對(duì)象添加字段
    let array = [{id:1,name:"靚仔",age:18},{id:2,name:"靚女",age:18},{id:3,name:"瘦子",age:16},{id:4,name:"肥仔",age:28}]
? ? ?    function filterAddArea (array=[]){
? ? ? ?     // 年齡 18
? ? ? ?     return array.map(ele=>{
? ? ? ? ?     ? ? return {...ele,...{area:'廣東'}}
? ? ? ?     })
? ? ?    }
? ? ?    let? prams =??filterAddArea(array)? // [{id:1,name:"靚仔",age:18,?area:?'廣東'},{id:2,name:"靚女",age:18,?area:?'廣東'},{id:3,name:"瘦子",age:16,?area:?'廣東'},{id:4,name:"肥仔",age:28,?area:?'廣東'}]
3. 對(duì)象的過濾
  var obj = {a:1,b:2,c:3}
  let { a, ...prams } = obj
  console.log(prams) //?{b:2,c:3}
?
?
?
?

本文摘自 :https://www.cnblogs.com/

開通會(huì)員,享受整站包年服務(wù)立即開通 >