js 用迭代器模式優雅的處理遞歸問題

2019獨角獸企業重金招聘Python工程師標準>>> hot3.png

什么是迭代器

循環數組或對象內每一項值,在 js 里原生已經提供了一個迭代器。

var arr = [1, 2, 3]
arr.forEach(function (item) {console.log(item)
})

實現一個迭代器

var iterator = function (arr, cb) {for (let i = 0; i < arr.length; i++) {cb.call(arr[i], arr[i], i)}
}var cb = function (item, index) {console.log('item is %o', item)console.log('index is %o', index)
}var arr = [1, 2, 3]
iterator(arr, cb)/*
打印:
item is 1
index is 0
item is 2
index is 1
item is 3
index is 2
*/

實際應用

需求:

  • Antd 的嵌套表格組件的數據源有要求,如果沒有子元素,children 屬性應該設置為 null 或者刪除 children 屬性,實際開發中后端返回的接口卻是沒有子元素時,children 屬性設置為一個空數組;
  • 后端返回的字段名 categoryId 字段名更改為 value,name 字段名更改為 label。

數據結構修改前后示例。

var categoryList = [{categoryId: 1,name: '1級',children: [{categoryId: 11,name: '11級',children: [],},],},{categoryId: 2,name: '2級',children: []}
]// 處理之后數據結構如下var categoryList = [{value: 1,label: '1級',children: [{value: 11,label: '11級',},],},{value: 2,label: '2級',}
]

使用迭代器模式優雅的處理遞歸類問題。

// 數據源
var categoryList = [{categoryId: 1,name: '1級',children: [{categoryId: 11,name: '11級',children: [],},],},{categoryId: 2,name: '2級',children: []}
]// 迭代器
var iterator = function (arr, cb) {for (let i = 0; i < arr.length; i++) {cb.call(arr[i], arr[i])}
}// 處理每一項
var changeItem = function (item) {// 更改字段名稱item.value = item.categoryIditem.label = item.namedelete item.categoryIddelete item.name// 當 children 為空數組時,刪除 children 屬性if (item.children == false) {delete item.children} else {iterator(item.children, changeItem)}
}// 調用迭代器
iterator(categoryList, changeItem)
console.log(JSON.stringify(categoryList, null, 4))/*
打印:
[{"children": [{"value": 11,"label": "11級"}],"value": 1,"label": "1級"},{"value": 2,"label": "2級"}
]
*/

總結

凡是需要用到遞歸的函數參考迭代器模式,能寫出更優雅,可讀性更高的代碼。

轉載于:https://my.oschina.net/dkvirus/blog/3031505

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/275439.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/275439.shtml
英文地址,請注明出處:http://en.pswp.cn/news/275439.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

如何抓取html請求,請求獲取網頁的response,獲取網頁的html 怎么那么慢

HttpEntity multipart builder.build();httppost.setEntity(multipart);long start System.currentTimeMillis();// 發送請求response httpclient.execute(httppost);long end System.currentTimeMillis();System.out.println("查詢upsfreight消耗的時間是(毫秒):&quo…

Serverless 究竟是什么?

大家好&#xff0c;我是若川。說起 Serverless&#xff0c;我想你應該并不陌生&#xff0c;作為一種云開發的架構模式&#xff0c;在近兩年里&#xff0c;伴隨著云原生概念的推廣愈發火爆。作為一名 Serverless 的擁躉&#xff0c;在跟大家推薦的過程中&#xff0c;我經常能看到…

instagram.apk_評論:Instagram Reels vs.TikTok

instagram.apkWith all the attention to the newly debuted Instagram Reels from Facebook and the hilarious, bizarre world of TikTok, here’s a first impression on the two platforms and how they compare from a designer’s perspective.所有人都在關注Facebook新近…

240多個jQuery常用到的插件

概述 jQuery 是繼 prototype 之后又一個優秀的 Javascript 框架。其宗旨是—寫更少的代碼,做更多的事情。它是輕量級的 js 庫(壓縮后只有21k) &#xff0c;這是其它的 js 庫所不及的&#xff0c;它兼容 CSS3&#xff0c;還兼容各種瀏覽器&#xff08;IE 6.0, FF 1.5, Safari 2.…

華為首款鴻蒙設備正式入網,華為首款鴻蒙設備正式入網:麒麟9000+挖孔全面屏,價格感人!...

作為國內電子產品領域的巨頭之一&#xff0c;華為這兩年的快速發展是大眾有目共睹的&#xff0c;除了手機業務外&#xff0c;華為的平板業務同樣有亮眼表現&#xff0c;無獨有偶&#xff0c;在近期各方媒體的不斷披露之下&#xff0c;又有一款華為平板被基本確認&#xff0c;這…

早上讀英語

#早上讀英語# Keep your eyes on the stars and your feet on the ground. 轉載于:https://www.cnblogs.com/da3j/p/10646531.html

myeclipse深色模式_完善深色模式的調色板

myeclipse深色模式Apps largely have a limited color palette which may already map well to dark mode. However, some colors produce optical vibrations when viewed on a dark background, straining the user’s eyes. So, certain apps need to map to a slightly des…

微軟悄悄發布了 Web 版的 VsCode

大家好&#xff0c;我是若川&#xff0c;最近組織了源碼共讀活動《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》&#xff0c;感興趣的可以加我微信 ruochuan12 參與&#xff0c;長期交流學習。在8月31日&#xff0c;微軟發了一個介紹他們新發布的功能的帖子介紹&#…

html中樣式表的三種形式,CSS樣式表有幾種存在方式

外部樣式&#xff1a;將網頁鏈接到外部樣式表。內頁樣式&#xff1a;在網頁上創建嵌入的樣式表。行內標簽樣式&#xff1a;應用內嵌樣式到各個網頁元素標簽內。每一種方法均有其優缺點&#xff1a;當要在站點上所有或部份的網頁上一致地應用相同樣式時&#xff0c;可使用外部樣…

figma設計_設計原型的最簡單方法:Figma速成課程

figma設計It doesn’t matter if you haven’t used any prototyping tools before or you’re transitioning from other ones (like Sketch, Adobe XD); This guide is for beginners and professionals alike. So for a university assignment, I had to prepare a presenta…

《大話數據結構》讀后總結(九)

線性表 順序存儲結構的插入與刪除 獲得元素操作 #define OK 1 #define ERROR 0 #define TRUE 1 #define FALSE 0 typedef int Status; /* Status是函數的類型&#xff0c;其值是函數結果狀態代碼&#xff0c;如OK等 */ /* 初始條件&#xff1a;順序線性表L已存在&#xff0c;1≤…

關于router name 的url重寫 --frontname rewrite frontname重寫!

對于tag 也就是在url中有tag的url&#xff0c;希望改成wholesale等其他方式&#xff0c; 1 參看blog插件方式&#xff1a; 在etc/config.xml中添加事件&#xff1a; <events> <sitemap_add_xml_block_to_the_end> <observers> …

初中級工程師如何快速成長和尋求突破

大家好&#xff0c;我是若川&#xff0c;最近組織了源碼共讀活動《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》&#xff0c;已經有超50人提交了筆記&#xff0c;群里已經有超1200人&#xff0c;感興趣的可以加我微信 ruochuan12 參與。前言寫這篇文章的初衷是因為看到…

ajax使用html()后樣式無效,jquery.ajax使用字符串拼接后內聯css樣式失效

問題所在:是這樣的,我使用ajax調用了一串json數據,使用字符串拼接的方法動態插入div容器.結果css并沒有對動態插入的內容加css樣式.代碼描述:css使用的內聯,在head部分, jquery使用外聯,在body后.我嘗試過:$(function(){}) //入口函數加載window.onload function(){} //原生do…

ios 按鈕圖片充滿按鈕_iOS有一些非常危險的按鈕-UX評論

ios 按鈕圖片充滿按鈕I recently bought a cool thing off Amazon. It’s an adapter for iPhone, making it easy to transfer photos from your big bulky camera to your phone. The adapter itself is very easy to use: simply insert your SD card and plug the adapter …

URLScan工具配置方法第1/2頁

本文分步說明如何配置 URLScan 工具以防止 Web 服務器受到攻擊和利用。 如何配置 URLScan 工具察看本文應用于的產品文章編號 : 326444最后修改 : 2007年3月14日修訂 : 5.3我們強烈建議所有運行 Microsoft Windows Server 2003 的用戶將 Microsoft Internet 信息服務 (IIS) 升級…

poj 1809

///一個點的坐標只有四種可能&#xff0c;用0表示 偶數&#xff0c;1表示奇數 &#xff0c;則四種可能為&#xff08;0,0&#xff09;&#xff0c;&#xff08;0,1&#xff09; ///&#xff08;1,0&#xff09;&#xff0c;&#xff08;1,1&#xff09;。觀察公式A|x1y2 - y1…

swiftui_SwiftUI的混合包

swiftui介紹 (Introduction) SwiftUI introduced us to a whole new way of designing and coding interfaces. Gone are the old ways of subclassing UIKit (or AppKit) classes and hardwiring layout constraints. Instead, we now have a nice, declarative way of struct…

三年經驗前端社招——有贊

大家好&#xff0c;我是若川&#xff0c;祝大家中秋節快樂。最近組織了源碼共讀活動《1個月&#xff0c;200人&#xff0c;一起讀了4周源碼》&#xff0c;已經有超50人提交了筆記&#xff0c;群里已經有超1200人&#xff0c;感興趣的可以點此鏈接掃碼加我微信 ruochuan12 參與。…

html的 button點擊事件無效,InfoWindow里面加button,監聽button點擊事件無效 求解啊...

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓點擊infoWindw中的button按鈕&#xff0c;無效果&#xff1b;覆蓋默認的dom結構html,body,#container {width: 100%;height: 100%;margin: 0px;}p.my-desc {margin: 5px 0;line-height: 150%;}//創建地圖var map new AMap.Map(con…