Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Help needed to flatten nest array within a object #201

Open
imranmomin opened this issue Sep 2, 2020 · 0 comments
Open

Help needed to flatten nest array within a object #201

imranmomin opened this issue Sep 2, 2020 · 0 comments

Comments

@imranmomin
Copy link

Hi,

I'm trying to use the library to map the below source to a flat version

Source

{
  "id": "2",
  "type": "plan",
  "name": "silver",
  "#s": [
    {
      "amount": {
        "currency": "cad",
        "value": 5.99
      },
      "interval": {
        "type": 1,
        "duration": 30
      }
    },
    {
      "amount": {
        "currency": "cad",
        "value": 9.99
      },
      "interval": {
        "type": 2,
        "duration": 90
      }
    }
  ]
}

Target

{
       "id": "2",
       "name": "silver",
       "amount": {
           "currency": "cad",
           "value": 5.99
       },
       "interval": {
           "type": 1,
           "duration": 30
       }
   },
   {
       "id": "2",
       "name": "silver",
       "amount": {
           "currency": "cad",
           "value": 9.99
       },
       "interval": {
           "type": 2,
           "duration": 90
       }
   }

To achieve the above i'm using flatMap function. I tried couple of ways using StrictSchema

const result = plans.flatMap(x => {
    const plan = {
        id: x.id,
        name: x.name
    }

    return x.#s.map(p => <Plan>{
        ...plan,
        amount: p.amount,
        interval: p.interval
    });
});

Also, many times I need to do reverse mapping i.e. from Target to Source and for that I use reduce function

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant