0

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.

3
  • Is your first snippet two different variables? Or is that one long string? Because that's invalid JSON Commented Jan 2, 2020 at 20:00
  • Please check now. I updated my snippet in question. thanks Commented Jan 2, 2020 at 20:03
  • 1
    Ah, so you have an array of arrays with an object in them. You're looking for a flatten in lodash if they have it. lodash.com/docs/2.4.2#flatten Commented Jan 2, 2020 at 20:04

3 Answers 3

3

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; }

Sign up to request clarification or add additional context in comments.

Comments

2

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

1 Comment

this is perfect. Thanks a lot
-1

This is the solution using destructuring

const arr1 = [[{......}], [{......}]]

const result = [...arr1[0], ...arr1[1]]

1 Comment

It's an array of arrays

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.