Reputation: 1451
I have a result like this.
[ [ { _id: 5e0e46938d6bb7459407ad8b,
accountId: 58dc5b9f3107602dbaba1281,
buildingId: 5d254bb179584ebcbb68b712,
gatewayId: 5d254b64ba574040d9632ada,
deviceId: 5d25f9d2dc4aea7838b0aaa1,
movementIndex: 2.437685743915776,
fallStatus: 6,
breathingRate: 21,
breathingStatus: 1,
presenceStatus: 1,
heartRate: 88,
heartRateStatus: 1,
noMovementPeriod: 3,
__v: 0,
createdAt: 2020-01-02T19:37:55.423Z,
updatedAt: 2020-01-02T19:37:55.423Z } ],
[ { _id: 5e0e46678d6bb7459407ad76,
accountId: 58dc5b9f3107602dbaba1281,
buildingId: 5d254bb179584ebcbb68b712,
gatewayId: 5d254b64ba574040d9632ada,
deviceId: 5d25f9d2dc4aea7838b0aaa2,
movementIndex: 2.079566889716151,
fallStatus: 1,
breathingRate: 18,
breathingStatus: -1,
presenceStatus: 0,
heartRate: 92,
heartRateStatus: -1,
noMovementPeriod: 2,
__v: 0,
createdAt: 2020-01-02T19:37:11.972Z,
updatedAt: 2020-01-02T19:37:11.972Z } ],
]
As you can see they are separate objects. I want them to be in one single object as elements so I can have only one array of object.
Expected result -
[ { _id: 5e0e46938d6bb7459407ad8b,
accountId: 58dc5b9f3107602dbaba1281,
buildingId: 5d254bb179584ebcbb68b712,
gatewayId: 5d254b64ba574040d9632ada,
deviceId: 5d25f9d2dc4aea7838b0aaa1,
movementIndex: 2.437685743915776,
fallStatus: 6,
breathingRate: 21,
breathingStatus: 1,
presenceStatus: 1,
heartRate: 88,
heartRateStatus: 1,
noMovementPeriod: 3,
__v: 0,
createdAt: 2020-01-02T19:37:55.423Z,
updatedAt: 2020-01-02T19:37:55.423Z },
{ _id: 5e0e46678d6bb7459407ad76,
accountId: 58dc5b9f3107602dbaba1281,
buildingId: 5d254bb179584ebcbb68b712,
gatewayId: 5d254b64ba574040d9632ada,
deviceId: 5d25f9d2dc4aea7838b0aaa2,
movementIndex: 2.079566889716151,
fallStatus: 1,
breathingRate: 18,
breathingStatus: -1,
presenceStatus: 0,
heartRate: 92,
heartRateStatus: -1,
noMovementPeriod: 2,
__v: 0,
createdAt: 2020-01-02T19:37:11.972Z,
updatedAt: 2020-01-02T19:37:11.972Z } ]
I am trying to figure out a lodash but not getting any result. Your help is very much appreciated. Thanks.
Upvotes: 0
Views: 61
Reputation: 5055
You can just use .flat()
and it will work fine for you, no need to loop
const data = [[{_id:"5e0 e46938d6bb7459407ad8b",accountId:"58 dc5b9f3107602dbaba1281",buildingId:"5 d254bb179584ebcbb68b712",gatewayId:"5 d254b64ba574040d9632ada",deviceId:"5 d25f9d2dc4aea7838b0aaa1",movementIndex:2.437685743915776,fallStatus:6,breathingRate:21,breathingStatus:1,presenceStatus:1,heartRate:88,heartRateStatus:1,noMovementPeriod:3,__v:0,createdAt:"2020 - 01 - 02 T19: 37: 55.423 Z",updatedAt:"2020 - 01 - 02 T19: 37: 55.423 Z"}],[{_id:"5e0 e46678d6bb7459407ad76",accountId:"58 dc5b9f3107602dbaba1281",buildingId:"5 d254bb179584ebcbb68b712",gatewayId:"5 d254b64ba574040d9632ada",deviceId:"5 d25f9d2dc4aea7838b0aaa2",movementIndex:2.079566889716151,fallStatus:1,breathingRate:18,breathingStatus:-1,presenceStatus:0,heartRate:92,heartRateStatus:-1,noMovementPeriod:2,__v:0,createdAt:"2020 - 01 - 02 T19: 37: 11.972 Z",updatedAt:"2020 - 01 - 02 T19: 37: 11.972 Z"}]];
const newData = data.flat();
console.log(newData);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Upvotes: 3
Reputation: 2146
This is the solution using destructuring
const arr1 = [[{......}], [{......}]]
const result = [...arr1[0], ...arr1[1]]
Upvotes: -1
Reputation: 11571
Your original post is a bit unclear, but I think you have an array, inside of which are subarrays with a single object in them each? Sort of like this?
var foo = [
[ { bar: "baz" } ],
[ { bar: "qux" } ]
];
There is no syntactic sugar ("destructuring" shorthand) you can do to flatten that, that I know of. But it's quite simple with Array.map()
:
var baz = foo.map(ea => ea[0]);
// baz = [ { bar: "baz" }, { bar: "qux"} ]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
Upvotes: 2