最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Immutable.Js - Add new property in Map - Stack Overflow

programmeradmin10浏览0评论

So let's say that I make a REST API call and receive a JSON in this format:

{
    "plan": {
        "createdDate": "05/04/2017",
        "location": {
          "city": null
          "state": null
        }
        "family": null
    }
}

And within in my reducer, make the JSON as an immutable as so:

Immutable.Map(Immutable.fromJS(action.json))

Along the way, is it possible to add properties under family?

For instance, when I am submitting a form to a server, ideally I like to send something like this:

{
    "plan": {
        "createdDate": "05/04/2017",
        "location": {
          "city": null
          "state": null
        }
        "family": {
          "father": "Homer",
          "mother": "Marge",
          "son": "Bartholomew"
        }
    }
}

Or do I have to initialized first to have those properties under family from the start?

So let's say that I make a REST API call and receive a JSON in this format:

{
    "plan": {
        "createdDate": "05/04/2017",
        "location": {
          "city": null
          "state": null
        }
        "family": null
    }
}

And within in my reducer, make the JSON as an immutable as so:

Immutable.Map(Immutable.fromJS(action.json))

Along the way, is it possible to add properties under family?

For instance, when I am submitting a form to a server, ideally I like to send something like this:

{
    "plan": {
        "createdDate": "05/04/2017",
        "location": {
          "city": null
          "state": null
        }
        "family": {
          "father": "Homer",
          "mother": "Marge",
          "son": "Bartholomew"
        }
    }
}

Or do I have to initialized first to have those properties under family from the start?

Share Improve this question asked May 10, 2017 at 21:58 AlejandroAlejandro 2,3247 gold badges39 silver badges64 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

You can follow this way via mergeDeep of Immutable:

const { fromJS } = require('[email protected]')

const initialState = fromJS({
    "plan": {
        "createdDate": "05/04/2017",
        "location": {
          "city": null,
          "state": null
        },
        "family": null
    }
})

const newState = initialState.mergeDeep({
    "plan": {
        "family": {
            "father": "Homer",
            "mother": "Marge",
            "son": "Bartholomew"
        }
    }
})

console.log(newState.toJS())

You will got this result

{
    "plan": {
        "createdDate": "05/04/2017",
        "location": {
          "city": null,
          "state": null
        },
        "family": {
            "father": "Homer",
            "mother": "Marge",
            "son": "Bartholomew"
        }
    }
}

Hope this can help you for solving this same issue with me, I was succeed!

Just to plement @julien-deniau answer. In case you have already properties in your object, you can use mergeIn. For example:

{
"plan": {
    "createdDate": "05/04/2017",
    "location": {
      "city": null
      "state": null
    }
    "family": {
      "father": "Homer",
      "mother": "Marge",
      "son": "Bartholomew"
      ---> you need to add smthg here
    }
}

}

state.mergeIn([
    'plan',
    'family'], {grandfather:'Abraham Jedediah'});

For the record, you don't need to do

const m = Immutable.Map(Immutable.fromJS(action.json))

But

const m = Immutable.fromJS(action.json)

does the job

Then you can just do:

const n = m.setIn(['plan', 'family'], /* your family object */)

Here :

const n = m.setIn(['plan', 'family'], Immutable.Map({ father: 'Homer', mother: 'Marge', son: 'Bartholomew' }))
发布评论

评论列表(0)

  1. 暂无评论