【问题标题】:Immutable.js turn nested array in immutable MapImmutable.js 在不可变 Map 中转嵌套数组
【发布时间】:2016-10-05 19:45:33
【问题描述】:

我没有得到关于以下问题的解决方案。我希望这对其他一些开发人员有用。我正在尝试了解 Immutable.js。我正在写一个测试和一个函数,但我没有得到想要的结果,我不明白我应该怎么做......

-> 我有嵌套的数组成员 -> 我应该在下面的函数中添加什么来将该测试数组转换为不可变的嵌套映射? -> 我应该期待什么结果?

我有这个功能:

import {List, Map, fromJS} from 'immutable';

export function setInitial(state, infos, members) {
  return state.set('infos', Map(infos))
              .set('members', ?????(members));
}

...还有这个测试...

import {List, Map} from 'immutable';
import {expect} from 'chai';

import {setInitial,
        addAtListOfMembers,
        removeAtListOfMembers
        } from '../src/core';

describe('application logic', () => {

    describe('setInitial', () => {

        it('charge the infos of the laboratory', () => {
          const state = Map();
          const infos = {
            id:1,
            laboName: 'Magenta Service',
            phone: '+331650042958',
            mail: 'email@magentaservice.fr',
            date: '12/5/2014'
          };

        const members = [
            {
                id:1,
                userName:'René',
                date:'12/02/2016'
            },
            {
                id:2,
                userName:'Jean',
                date:'10/03/2016'
            }
         ];

          const nextState = setInitial(state, infos, members);
          expect(nextState).to.equal(Map({
            infos : Map({
                id:1,
                laboName: 'Magenta Service',
                phone: '+331650042958',
                mail: 'email@magentaservice.fr',
                date: '12/5/2014'
              }),

           // =>MAYBE ???

            members: List.of(
                Map({
                    id:1,
                    userName:'René',
                    date:'12/02/2016'
                }),
                Map({
                    id:2,
                    userName:'Jean',
                    date:'10/03/2016'
                })
              );
          }));
        });
    });
});

提前感谢您的回答!

【问题讨论】:

标签: javascript arrays immutable.js


【解决方案1】:

Immutable.fromJS() 应该做你想做的事。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 2017-01-15
    相关资源
    最近更新 更多