From ad7e550f71f6d3108fa510f3e378e1d2c532f864 Mon Sep 17 00:00:00 2001 From: chenkang <18383376972@163.com> Date: Mon, 6 Apr 2020 12:47:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat:=20Children=20map=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d8..4e64621 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,8 +1,29 @@ function mapChildren(children, func, context) { - //TODO实现此mapChildren方法 - return children; + //TODO实现此mapChildren方法 + // return children; + let childArray = []; + let key = 0; + const renderChild = (children, index, flatKey) => { + if (index === children.length) { + return; + } + if (Array.isArray(children[index])) { + renderChild(children[index], 0, `${flatKey}${index}:`); + } else { + let currentChild = children[index]; + let callBackData = func(currentChild, key); + callBackData.forEach((element) => { + let childKey = currentChild.key ? `$${currentChild.key}` : index; + element.key = `${flatKey}${childKey}/.$${element.key}`; + childArray.push(element) + }); + key++; + return renderChild(children, ++index, flatKey); + } + + }; + renderChild(children, 0, "."); + return childArray; } -export { - mapChildren as map, -}; \ No newline at end of file +export { mapChildren as map }; -- Gitee From 6c8044704e6ce825f828b95e78f49a696fae7b10 Mon Sep 17 00:00:00 2001 From: chenkang <18383376972@163.com> Date: Mon, 6 Apr 2020 12:52:43 +0800 Subject: [PATCH 2/2] =?UTF-8?q?feat:=20Children=20map=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index 4e64621..038719e 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -20,7 +20,6 @@ function mapChildren(children, func, context) { key++; return renderChild(children, ++index, flatKey); } - }; renderChild(children, 0, "."); return childArray; -- Gitee