美文网首页
练习记录(递归,扁平化)

练习记录(递归,扁平化)

作者: 糖醋里脊120625 | 来源:发表于2022-03-15 14:28 被阅读0次
 <script>
    /**
  
    let obj = {username: 'kobe', age: 39, sex: {option1: '男', option2: '女'}};
      let obj1 = obj;
      obj1.sex.option1 = '不男不女'; // 修改复制的对象会影响原对象
      console.log(obj1, obj);
      console.log(obj1, obj);
      
    
   
   let obj = { username:'kobe'};
   let obj2 = Object.assign(obj);
   obj2.username = 'wade';
   console.log(obj);//{username: "wade"}
    *
    */
   
   
   let newbuild=[ { "id": 1, "name": "estate", "title": "物业管理", "type": "nav", "leave": 1, "order": 1, "parent_id": 0, "url": "/estate" },
 { "id": 2, "name": "house", "title": "费用管理", "type": "nav", "leave": 2, "order": 1, "parent_id": 1, "url": "estate" },
 { "id": 3, "name": "temporaryCharges", "title": "临时收费", "type": "nav", "leave": 3, "order": 1, "parent_id": 2, "url": "charge" }, 
{ "id": 4, "name": "propertyFees", "title": "物业费", "type": "nav", "leave": 3, "order": 2, "parent_id": 2, "url": "propertyFees" },
 { "id": 6, "name": "propertyFeesEdit", "title": "编辑", "type": "handle", "leave": 4, "order": 2, "parent_id": 4, "url": null },
 { "id": 7, "name": "propertyFeesLogs", "title": "记录", "type": "handle", "leave": 4, "order": 3, "parent_id": 4, "url": null },
 { "id": 8, "name": "propertyFeesPayment", "title": "缴费", "type": "handle", "leave": 4, "order": 4, "parent_id": 4, "url": null },
 { "id": 9, "name": "carFees", "title": "停车费", "type": "nav", "leave": 3, "order": 3, "parent_id": 2, "url": "carFees" },
 { "id": 11, "name": "carFeesLogs", "title": "记录", "type": "handle", "leave": 4, "order": 2, "parent_id": 9, "url": null },
 { "id": 12, "name": "carFeesPayment", "title": "缴费", "type": "handle", "leave": 4, "order": 3, "parent_id": 9, "url": null },
 { "id": 13, "name": "gain", "title": "固定收益", "type": "nav", "leave": 3, "order": 4, "parent_id": 2, "url": "gain" },
 { "id": 14, "name": "bill", "title": "账单管理", "type": "nav", "leave": 2, "order": 2, "parent_id": 1, "url": "estate" },]
 
 function compare(property) {
     return function(a, b) {
       const value1 = a[property];
       const value2 = b[property];
       return value1 - value2;// 升序,降序为value2 - value1
     };
   }
 
 function treedata(data){
      // 对源数据深度克隆
         const cloneData = JSON.parse(JSON.stringify(data));
         // filter嵌套filter相当于for循环嵌套for循环
         const result = cloneData.filter(parent => {
           // 返回每一项的子级数组
           const branchArr = cloneData.filter(child => parent.id === child.parent_id);
     
           // 若子级存在,则给子级排序;且,赋值给父级
           if (branchArr.length > 0) {
             branchArr.sort(compare('order'));
             parent.children = branchArr;
           }
           // 返回最高的父级,即,parent_id为0,
           return parent.parent_id === 0;
         });
         // 给最高级的父级排序
         result.sort(compare('order'));
         console.log(result)
 }
 treedata(newbuild)
 
 
 let ary = [1, [2, [3, [4, 5]]], 6];
 let str = JSON.stringify(ary);
 let arr_flat = ary.flat(Infinity);
 console.log(arr_flat)
 console.log(ary.flat(3))
 
 
 
 //递归
 /**
  * 
  * 
  * 
  * 
  */
var arrList = [1,2,3,5,100,500,10000,10000,1000,10000002]
 //for循环测试
 function forTest(){
     console.time("for循环")
     for(let i in arrList){
         console.log(((arrList[i] + arrList[i]) * 5 - arrList[i])/arrList[i])
     }
     console.timeEnd("for循环")
 }
 //递归遍历测试
 function recursive() {
     console.time("递归遍历")
     const testFun = function (i) {
         console.log(((arrList[i] + arrList[i]) * 5 - arrList[i])/arrList[i])
         if(i == arrList.length - 1){
             return
         }
         i++
         testFun(i)
     }
     testFun(0)
     console.timeEnd("递归遍历")
 }
 forTest()
 recursive()
 
 
 
 
 var dataforEach = [
  {
      name: "所有物品",
      children: [
          {
              name: "水果",
              children: [{name: "苹果", children: [{name: '青苹果'}, {name: '红苹果'}]}]
          },
          {
              name: '主食',
              children: [
                  {name: "米饭", children: [{name: '北方米饭'}, {name: '南方米饭'}]}
              ]
          },
          {
              name: '生活用品',
              children: [
                  {name: "电脑类", children: [{name: '联想电脑'}, {name: '苹果电脑'}]},
                  {name: "工具类", children: [{name: "锄头"}, {name: "锤子"}]},
                  {name: "生活用品", children: [{name: "洗发水"}, {name: "沐浴露"}]}
              ]
          }
   ]
 }]
 
 var forFunction = function () {
     var str = ""
     dataforEach.forEach(function(row){
         row.children.forEach(function(row){
             row.children.forEach(function(row){
                 row.children.forEach(function(row){
                     str += (row.name + ";")
                 })
             })
         })
     })
     console.log(str)
 }
 forFunction()
 
 var recursiveFunction = function(){
     var str = ''
     const getStr = function(list){
         list.forEach(function(row){
             if(row.children){
                 getStr(row.children)
             }else {
                 str += row.name + ";"
             }
         })
     }
     getStr(dataforEach)
     console.log(str)
 }
 recursiveFunction()
 
 
 
 
 
 
 </script>

相关文章

  • 练习记录(递归,扁平化)

  • js经典算法记录

    随机数组洗牌 简单的日期字符串排序 递归实现数组扁平化 极简版数组扁平化 记录数组项重复次数 冒泡排序 快速排序(...

  • 用JavaScript实现的5个常见函数

    数组扁平化 数组扁平化有很多方法,但最终最好的方法就是递归,实现一个指定深度的扁平化方法,这样基本的套路都会了解。...

  • 【转】js数组和树结构数据相互转换

    数组转树结构采取递归和非递归两种方式,树结构转扁平化数组采取深度优先遍历(递归和非递归两种方式)和广度优先遍历实现...

  • js数组扁平化

    扁平化 数组的扁平化,就是将一个嵌套多层的数组 array (嵌套可以是任何层数)转换为只有一层的数组。 递归 e...

  • 数组扁平化-2019-12-17

    数组扁平化 总结 有5种方法:2013 递归 or toString() 2018 flat()其它的还有 so...

  • 数组扁平化,柯里化,防抖,节流,对象拷贝

    数组扁平化 数组扁平化:使用递归实现 将每一项遍历,如果某一项为数组,则让该项继续调用,这里指定了depth作为扁...

  • 2020前端面试(数据结构)

    常见排序算法 冒泡排序 快速排序 选择排序 插入排序 数组扁平化 递归 reduce toString 树的遍历 ...

  • Javascript数组扁平化

    数组的扁平化是指将多维数组转换成一位数组。 递归方法 用数组的reduce方法来简化递归操作 用数组的toStri...

  • js扁平化

    扁平化就是将数组依次递归处理,将其转换成一阶数组,例如:【1,【2】,【【3,4】】,【【【5】】】】------...

网友评论

      本文标题:练习记录(递归,扁平化)

      本文链接:https://www.haomeiwen.com/subject/bflydrtx.html