繁体   English   中英

这种 arrays 如何展平?

[英]How to flatten this kind of arrays?

我正在尝试展平一个数组,但在这方面遇到了一些问题。 我有这些数据可以展平。

   arr =  [
    {
        "data": [
            [
                {
                    "_id": "5ee97ee7f25d1c1482717bdf",
                    "email": "test1@test.io",
                    "profileImages": [],
                    "username": "test1",
                    "birthday": "2020-06-11T10:11:32.000Z",
                    "phoneNumber": "+910000000000",
                    "location": "Test Location",
                    "firstName": "test1",
                    "lastName": "test1",

                }
            ],
            [
                {
                    "_id": "5ee97ef2f25d1c1482717be1",
                    "email": "test2@test.io",
                    "profileImages": [],
                    "username": "test2",
                    "birthday": "2020-06-11T10:11:32.000Z",
                    "phoneNumber": "+910000000000",
                    "location": "Test Location"
                }
            ]
        ]
    }
],

这就是我所拥有的......并且我希望这些数据以某种方式将数据合并到一个数组中,如下面的结构

data: [
           {
                "_id": "5ee97ee7f25d1c1482717bdf",
                "email": "test1@test.io",
                "profileImages": [],
                "username": "test1",
                "birthday": "2020-06-11T10:11:32.000Z",
                "phoneNumber": "+910000000000",
                "location": "Test Location",
                "firstName": "test1",
                "lastName": "test1"},
            {
                "_id": "5ee97ef2f25d1c1482717be1",
                "email": "test2@test.io",
                "profileImages": [],
                "username": "test2",
                "birthday": "2020-06-11T10:11:32.000Z",
                "phoneNumber": "+910000000000",
                "location": "Test Location"
            }
        ]

我曾尝试使用 lodash 库将其展平,但没有奏效。 对此有何建议,我如何将这些 arrays 展平为单个阵列?

您可以通过使用flatMapflat()的组合来做到这一点,flat 将展平内部数组和 flatMap 用于外部数组。

 var arr = [ { "data": [ [ { "_id": "5ee97ee7f25d1c1482717bdf", "email": "test1@test.io", "profileImages": [], "username": "test1", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location", "firstName": "test1", "lastName": "test1", } ], [ { "_id": "5ee97ef2f25d1c1482717be1", "email": "test2@test.io", "profileImages": [], "username": "test2", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location" } ] ] }]; var result = arr.flatMap(obj=>obj.data.flat()); console.log(result);

我希望这有帮助。

flatMapflat()并未在所有浏览器中得到广泛支持。 看到这个: 在多维数组中搜索(算法)

所以简单的递归是最安全的选择。

 var data = [ [ { "_id": "5ee97ee7f25d1c1482717bdf", "email": "test1@test.io", "profileImages": [], "username": "test1", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location", "firstName": "test1", "lastName": "test1", } ], [ { "_id": "5ee97ef2f25d1c1482717be1", "email": "test2@test.io", "profileImages": [], "username": "test2", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location" } ] ] var result = []; function flatten(data){ data.forEach(k=>{ if(Array.isArray(k)){ flatten(k) }else{ result.push(k) } }); return result; } console.log(flatten(data))

你可以用flat

 var arr = [ { "data": [ [ { "_id": "5ee97ee7f25d1c1482717bdf", "email": "test1@test.io", "profileImages": [], "username": "test1", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location", "firstName": "test1", "lastName": "test1", } ], [ { "_id": "5ee97ef2f25d1c1482717be1", "email": "test2@test.io", "profileImages": [], "username": "test2", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location" } ] ] }]; console.log([...arr[0].data.flat()])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM