diff --git a/src/react/ReactChildren.js b/src/react/ReactChildren.js index bbc07d81edaad3705a3dca0942e1cbc01bff2624..c291ffa919874729ae04f681fbe6726c5a41971c 100644 --- a/src/react/ReactChildren.js +++ b/src/react/ReactChildren.js @@ -1,6 +1,38 @@ -function mapChildren(children, func, context) { +function mapChildren (children, func, context) { //TODO实现此mapChildren方法 - return children; + if (!Array.isArray(children)) children = [children]; + children = flattenArray(children, [], ':'); + // console.log(children) + let mappedChildren = []; + children.forEach((item, index) => { + // console.log(item, 'item'); + let resItem = flattenArray(func(item, index), [], '/', item.key); + mappedChildren.push(...resItem) + }); + // return mappedChildren; + return mappedChildren; +} +/* + .0/ + .$+key2+/ .$key2/ + [key4] .3:$key4/ +*/ + +function flattenArray (arr, res = [], split, key) { + // console.log(arr, 'arr') + if (!Array.isArray(arr)) res.push(arr); + arr.forEach((item, index) => { + // console.log(item, 'item') + if (key) { + if (item.key) item.key = `${key}${split}.$${item.key}` + else item.key = `${key}${split}.${index}` + } else { + if (item.key) item.key = `.$${item.key}` + else item.key = `.${index}` + } + Array.isArray(item) ? flattenArray(item, res, ':', item.key) : res.push(item); + }); + return res; } export {