前端js常用方法和事件

1.form表单触发submit事件去掉值为''和null的数据

function () {
  var formData = $('#search').serializeArray()
  var data = {}
  formData.map((item, index) => {
    if (item.value == 'null' || item.value == '') return
    data[item.name] = item.value
  })

2.递归渲染数据列表

recursionOutline($('元素接点'),'数据')
function recursionOutline(el, data) {
  el.empty()//先清空
  for (var i = 0; i < data.length; i++) {
    const element = data[i];
    if (element.childNode) {//包含子节点,这里的childNode是我的子节点属性
      el.append(`
  • ${element.doc_number + "." + element.doc_title}
    • `); recursionOutline(el.children("li").last().children("ul"), element.childNode);//递归渲染子节点 } else {//不包含子节点 el.append(`
    • ${element.doc_number + "." + element.doc_title}
    • `) } } };

      3.数据类型转换

      1)将数组转换成对象
      var arr= ['1', '2', '3', '4', '5', '6', '7', '8'];
      var obj= {...arr}
      console.log(obj); // {'0': '1','1': '2', '2': '3','3': '4','4': '5','5': '6','6': '7','7': '8'}
      
      2)将数组转换成字符串
      var arr= ['1', '2', '3', '4', '5', '6', '7', '8'];
      var str= arr.join('')
      console.log(str); // 12345678
      

      4.判断两个数组中的相同值

      var arr1= ['1', '2', '3', '4', '5', '6', '7', '8'];
      var arr2= ['1', '2', '3'];
      var array= [...new Set(arr1)].filter(item => arr2.includes(item));
      console.log(array); // [ '1', '2', '3' ]
      

      5.reduce的高级用法

      var arr1= ['1', '2', '3', '4', '5', '6', '7', '8'];
      var arr2= ['1', '2', '3'];
      var array= [...new Set(arr1)].filter(item => arr2.includes(item));
      console.log(array); // [ '1', '2', '3' ]
      
      1)计算数组中每个元素出现的次数
      let arr = ['a', 'c', 'd', 'b','a'];
      
      let objnum = arr.reduce((pre,cur)=>{
        if(cur in pre){
          pre[cur]++
        }else{
          pre[cur] = 1 
        }
        return pre
      },{})
      console.log(objnum); //{a: 2, c: 1, d: 1, b: 1}
      
      2)数组去重
      let arr = [1,2,3,4,4,1]
      let newArr = arr.reduce((pre,cur)=>{
          if(!pre.includes(cur)){
            return pre.concat(cur)
          }else{
            return pre
          }
      },[])
      console.log(newArr);// [1, 2, 3, 4]
      
      3)将单层嵌套数组合并
      let arr = [[0, 1], [2, 3], [4, 5]]
      let newArr = arr.reduce((pre,cur)=>{
          return pre.concat(cur)
      },[])
      console.log(newArr); // [0, 1, 2, 3, 4, 5]
      
      4)将多层嵌套数组转化为一维
      let arr = [[0, 1], [2, 3], [4,[5,6,7]]]
      const newArr = function(arr){
         return arr.reduce(
              (pre,cur)=>pre.concat(
              Array.isArray(cur)?newArr(cur):cur
              ),[])
      }
      console.log(newArr(arr)); //[0, 1, 2, 3, 4, 5, 6, 7]
      
      5)对象数组求和
      var result = [
          {
              subject: 'math',
              score: 10
          },
          {
              subject: 'chinese',
              score: 20
          },
          {
              subject: 'english',
              score: 30
          }
      ];
      
      var sum = result.reduce(function(prev, cur) {
          return cur.score + prev;
      }, 0);
      console.log(sum) //60
      

      你可能感兴趣的:(前端js常用方法和事件)