Angular Learner
Angular Learner

Reputation: 390

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"
    }]

Upvotes: 1

Views: 512

Answers (1)

Tushar
Tushar

Reputation: 87203

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>

Upvotes: 3

Related Questions