简体   繁体   中英

What are the lodash combinations to achieve the below scenario?

I get below data from twitter in which you can see it has multilevel array in it.I need a single array which has all the objects in it.You can find below how i need the data to be shown.

[
    [{
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }, {
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }],

    [{
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }, {
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }],

    [{
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }, {
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }]
]

but I need to loop only single array like this having all the data combined in single array.

[{
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
     }, {
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    {
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }, {
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    },
    {
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }, {
        "created_at": "Tue May 19 04:36:36 +0000 2015",
        "id": "asdfasdf",
        "id_str": "ASdfasdfasdf"
    }]

Use _.flatten(arr, isDeep)

Flattens a nested array. If isDeep is true the array is recursively flattened, otherwise it's only flattened a single level.

var flattenedArr = _.flatten(arr);

If your array is nested then use the true as second argument for deep flatten.

var flattenedArr = _.flatten(arr, true);

Demo

 var arr = [ [{ "created_at": "Tue May 19 04:36:36 +0000 2015", "id": "asdfasdf", "id_str": "ASdfasdfasdf" }, { "created_at": "Tue May 19 04:36:36 +0000 2015", "id": "asdfasdf", "id_str": "ASdfasdfasdf" }], [{ "created_at": "Tue May 19 04:36:36 +0000 2015", "id": "asdfasdf", "id_str": "ASdfasdfasdf" }, { "created_at": "Tue May 19 04:36:36 +0000 2015", "id": "asdfasdf", "id_str": "ASdfasdfasdf" }], [{ "created_at": "Tue May 19 04:36:36 +0000 2015", "id": "asdfasdf", "id_str": "ASdfasdfasdf" }, { "created_at": "Tue May 19 04:36:36 +0000 2015", "id": "asdfasdf", "id_str": "ASdfasdfasdf" }] ]; console.log(_.flatten(arr)); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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