From 4ac8a51ecee071a11ad51a6f2786b9a0911de590 Mon Sep 17 00:00:00 2001 From: wws2519 Date: Mon, 6 Apr 2020 19:41:50 +0800 Subject: [PATCH] update src/react/ReactChildren.js. --- 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..f16862f 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方法 + if (children == null) { + return children + } + let result = [] + let bookCount = 0; + const mapToKey = (children) => { + children.map((item, index) => { + if(Array.isArray(item)) { + mapToKey(item) + }else { + let mappedChild = func.call(context, item, bookCount) + if (Array.isArray(mappedChild)) { + result = result.concat(mappedChild) + } else { + result.push(mappedChild) + } + bookCount++; + } + }) + } + if (Array.isArray(children)) { + mapToKey(children) + } + return result } -export { - mapChildren as map, -}; \ No newline at end of file +export { mapChildren as map } \ No newline at end of file -- Gitee