From b66d3555d9f0e003c77ab397e79bce6a5e87ef41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E9=87=91=E6=B5=A9?= Date: Mon, 6 Apr 2020 22:12:19 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/react/ReactChildren.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d8..e85f900 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,6 +1,21 @@ function mapChildren(children, func, context) { //TODO实现此mapChildren方法 - return children; + const array = [] + children = Array.isArray(children) ? children : [children] + function fn(child, key, flag = false) { + child.forEach((item, index) => { + if (Array.isArray(item)) { + let newKey = key ? item.key ? key + '/' + item.key : flag ? key + `.${index}` : key + '/' + `.${index}` : `.${index}` + fn(item, newKey) + } else { + const res = func(item) + res.key = item.key ? key + '/' + `.$${item.key}` : flag ? key + `.${index}` : key + '/' + `.${index}` + array.push(res) + } + }) + } + fn(children, '', true) + return array; } export { -- Gitee