From bb2275acf5c7df4955ab60dd2b2f56634740b6e1 Mon Sep 17 00:00:00 2001 From: yehuozhili <673632758@qq.com> Date: Mon, 6 Apr 2020 20:53:20 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=A1=E9=A3=9E=E9=9B=84769?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 52 +++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d8..3db969a 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,7 +1,53 @@ -function mapChildren(children, func, context) { - //TODO实现此mapChildren方法 - return children; +function judgeKey(children,index,keyindex){ + let key =keyindex + if(children.key){ + key= key+`$${children.key}` + }else{ + key=key+`${index}` + } + return key } +function judgeSecond(key){ + let reg = new RegExp('/.') + return reg.exec(key) +} + +function mapChildren(children,func){ + let res = [] + let index=0 + function mapAll(children,key){ + for(let i in children){ + if(children.hasOwnProperty(i)){ + if(Array.isArray(children[i])){ + let newkey = key+i+`:` + mapAll(children[i],newkey) + }else{ + let newkey = judgeKey(children[i],i,key) + if(judgeSecond(newkey)){//只可能不是array + children[i].key =newkey + res.push(children[i]) + }else{ + let funcres = func(children[i],index) + index++; + if(Array.isArray(funcres)){ + let secKey= newkey+'/.' + console.log(secKey) + mapAll(funcres,secKey) + }else{//不返回数组会进来 + let mapedkey = judgeKey(funcres,index,newkey+'/.') + funcres.key =mapedkey + res.push(funcres) + } + } + } + } + + } + } + mapAll(children,'.') + return res +} + export { mapChildren as map, -- Gitee