diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d81edaad3705a3dca0942e1cbc01bff2624..98291ed29df19e05eec5908df33aa9ab1339fd27 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,6 +1,34 @@ function mapChildren(children, func, context) { //TODO实现此mapChildren方法 - return children; + if (!children) { + return [] + } + let arr = [], idx = 0, key = '' + function fn(children) { + if (Array.isArray(children)) { + for (let i = 0; i < children.length; i++) { + let child = children[i]; + child.key == null ? key = `.${index}/$` : key = `.$${child.key}/.$`; + if (Array.isArray(child)) { + fn(child); + } else { + callback(child); + idx++; + } + } + } else { + callback([children]) + } + } + function callback(children) { + let o = func(children, idx); + for (let k = 0; k < o.length; k++) { + o[k].key = key + o[k].key; + arr.push(o[k]) + } + } + fn(children); + return arr; } export {