From 1d4483cadec7e73f526ef1e963ff00abb80af9b2 Mon Sep 17 00:00:00 2001 From: "LAPTOP-IK5SDL4L\\Miller" Date: Sun, 5 Apr 2020 23:50:54 +0800 Subject: [PATCH] =?UTF-8?q?4.5=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d8..96d024c 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,6 +1,30 @@ function mapChildren(children, func, context) { //TODO实现此mapChildren方法 - return children; + children = Array.isArray(children) ? children: [children] + let newArr = []; + let index = 0; + function flatMap(kids, key){ + kids.forEach((kid, i) => { + if (Array.isArray(kid)) { + key += `${i}:` + flatMap(kid, key); + }else{ + const domkey = kid.key ? `$${kid.key}`: `${i}`; + let res = func(kid, index); + res = Array.isArray(res)? res: [res]; + res.forEach(child => { + child.key = `${key}${domkey}/.$${child.key? child.key: i}`; + newArr.push(child); + }); + index++; + } + }); + } + flatMap(children, '.'); + + // children = children.flat(Infinity).map(func).flat(); + + return newArr; } export { -- Gitee