打赏

二维数组展平为一维数组

<!DOCTYPE html>
<html lang="zh">

    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="ie=edge" />
        <title>二维数组展平为一维数组</title>
    </head>

    <body>
        <script type="text/javascript">
            function simpleNormalizeChildren(children) {
                for(let i = 0; i < children.length; i++) {
                    if(Array.isArray(children[i])) {
                        return Array.prototype.concat.apply([], children)
                    }
                }
                return children
            }
            console.log(simpleNormalizeChildren([1, [2, 3]]))
        </script>
    </body>

</html>

 

posted @ 2018-10-11 17:58  孟繁贵  阅读(1138)  评论(0编辑  收藏  举报
TOP