Yash
Yash

Reputation: 87

How to flatten this kind of arrays?

I am trying to flatten an array but facing some problem in that. I have this data available to be flattened.

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

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

this is what I have.... and I want this data in a way such that it should merge the data in a single array like this below structure

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

I had tried to use lodash library to flatten it but it didn't work. Any suggestions for this that how can i flatten these arrays to a single array?

Upvotes: 1

Views: 73

Answers (3)

Rajneesh
Rajneesh

Reputation: 5308

You can do this by making use of combination of flatMap and flat(), flat will flatten the inner array and flatMap for the outer array.

var  arr = [ { "data": [ [ { "_id": "5ee97ee7f25d1c1482717bdf", "email": "[email protected]", "profileImages": [], "username": "test1", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location", "firstName": "test1", "lastName": "test1", } ], [ { "_id": "5ee97ef2f25d1c1482717be1", "email": "[email protected]", "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);

I hope this helps.

Upvotes: 2

ABGR
ABGR

Reputation: 5235

flatMap and flat() is not widely supported across all the browser. See this: Search in multidimensional array (Algorithm)

So simple recursion is the safest bet.

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

                }
            ],
            [
                {
                    "_id": "5ee97ef2f25d1c1482717be1",
                    "email": "[email protected]",
                    "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))

Upvotes: 1

Sven.hig
Sven.hig

Reputation: 4519

you can use flat

var  arr = [ { "data": [ [ { "_id": "5ee97ee7f25d1c1482717bdf", "email": "[email protected]", "profileImages": [], "username": "test1", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location", "firstName": "test1", "lastName": "test1", } ], [ { "_id": "5ee97ef2f25d1c1482717be1", "email": "[email protected]", "profileImages": [], "username": "test2", "birthday": "2020-06-11T10:11:32.000Z", "phoneNumber": "+910000000000", "location": "Test Location" } ] ] }];

    console.log([...arr[0].data.flat()])

Upvotes: 0

Related Questions