diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d81edaad3705a3dca0942e1cbc01bff2624..39a2d8ce8213f3979ea4a037cff8506a0c4930eb 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,8 +1,45 @@ + +let arr = []; +let index = 0; +let key = ""; function mapChildren(children, func, context) { //TODO实现此mapChildren方法 - return children; + + if (children === null || children === undefined) { + return children; + } + function fn(children) { + if (Array.isArray(children)) { + for (let i = 0; i < children.length; i++) { + let child = children[i]; + if (child.key == null) { + key = `.${index}/$` + } else { + key = `.$${child.key}/.$`; + } + if (Array.isArray(child)) { + fn(child); + return; + } + callback(child); + index++; + } + } else { + callback(children) + } + } + function callback(child) { + let ary = func(child, index); + for (let k = 0; k < ary.length; k++) { + ary[k].key = key + ary[k].key; + arr.push(ary[k]) + } + } + fn(children); + return arr; } export { mapChildren as map, -}; \ No newline at end of file +}; +