From 681198255020bcb970fbf16bf2c0d3557661fc05 Mon Sep 17 00:00:00 2001 From: eamon Date: Wed, 8 Apr 2020 16:59:41 +0800 Subject: [PATCH] children.map zuoye --- src/react/ReactChildren.js | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d8..38a890e 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,8 +1,19 @@ function mapChildren(children, func, context) { - //TODO实现此mapChildren方法 - return children; + if (!children) return children; + let result = []; + mapChildren(children, func, context, result); + return result; } -export { - mapChildren as map, -}; \ No newline at end of file +function mapChildren(children, func, context, result) { + children.forEach((child, index) => { + if (Array.isArray(child)) { + return childrenMap(child, func); + } + func.call(context, child, index).forEach((item, ind) => { + item.key = "." + (child.key || index) + "/." + (item.key || ind); + result.push(key); + }); + }); +} +export { mapChildren as map }; -- Gitee