Search code examples
javascriptobjectecmascript-6reduxnormalizr

Normalizr: How to extract values and give it the object directly? How to reduce the object to the values of it's keys?


I know my question is formulated weirdly, but I don't know how to express it correctly. I want to normalize my data using normalizr for Redux. Let me state my question using code.

I have an api response looking like this:

[
  {
    "name": <some-name>,
    "id": <some-id>,
    "address": {
      "country": {
        "country": "<some-country-code>"
      },
      "state": {
        "name": "<some-state-name>"
      },
      ...
   }
]

As you can see, the address object contains nested objects, which only have one key.

In the normalized state, I would like this data to be reduced, so that in address it just says something like this:

"address": {
  "country": "<some-country-code>",
  "state": "<some-state-name>"
}

How would I go about doing this using normalizr? Is this possible?


Solution

  • Use a processStrategy to change data when an entity is processed during normalization: https://github.com/paularmstrong/normalizr/blob/6c5af279cc890a94fb7cde53a32811ccc7c2e28b/docs/api.md#entitykey-definition---options--