diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d81edaad3705a3dca0942e1cbc01bff2624..f16862f38e8caeb1989a4f9e305d74e31c8c5f25 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