• 利用map reduce方法将对象转成数组


    map 操作

    <!DOCTYPE html>
    <html lang="en">
    <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>利用map reduce将对象转为数组并且拉平</title>
    </head>
    <body>
        <script>
            var keys = ['a','b','c']
            //js Map 方法
            var dataObj = {
                a:[1],
                b:[2],
                c:[3,5]
            }
            console.log(keys.map(key => dataObj[key]))//[[1],[2],[3,5]]
            var result = keys.map(key => dataObj[key])
                .reduce((prev,next) => prev.concat(next),[])
            console.log(result)  //[1,2,3,5]
        </script>
    </body>
    </html>
  • 相关阅读:
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    个人博客
    5.14
    5.13
    5.12
  • 原文地址:https://www.cnblogs.com/pengfei25/p/14411677.html
Copyright © 2020-2023  润新知