diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d81edaad3705a3dca0942e1cbc01bff2624..038719eee7a9c007639e99d231d1296344d6ce2f 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,8 +1,28 @@ 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 };