如何在immutablejs中获取列表哈希映射的合并集嵌套数组

时间:2015-07-13 14:10:27

标签: javascript immutable.js

这段代码有什么作用?

  const repos = state.get(`users/${username}`);
    const nextRepos = repos.concat(Immutable.fromJS(res.body));

    return state.merge({
      [`users/${username}__res`]: res,
      [`users/${username}`]: nextRepos
    });

取自:https://github.com/quangbuule/redux-example/blob/master/src/js/reducers/Repo.js

get('users/1')merge('users/1')如何运作。

最终我有这个结构:

let state = {
  sequence: [
    [{column:3}],
    [{column: 3}]
  ]
}
state = Immutable.fromJS(state);

如何修改此状态,即如何添加新项目以便我得到此信息:

{ sequence: [
   [{column:3},{column:1}],
   [{column:3}]
}

1 个答案:

答案 0 :(得分:1)

结果使用setIn, mergeIn, updateIn等方法对嵌套结构非常有用:

      // get carousel "slide" active image
$("#carousel-example-generic").on('slide.bs.carousel', function() {

    var active_img = 'url(' + $('.active img').attr("src") + ')';
    // fades out active image
    $('#background-img').css('background-image', active_img).animate({ opacity: 0 }, { duration: 1000 });
});


  // get carousel "slid" active image
$("#carousel-example-generic").on('slid.bs.carousel', function() {

    var active_img = 'url(' + $('.active img').attr("src") + ')';
    // fades in "slid" active image
    $('#background-img').css('background-image', active_img).animate({ opacity: 0.3 }, { duration: 300 });
});

更新项目,使其具有以下结构:

let state = {
  sequence: [
    [{column:3}],
    [{column: 3}]
  ]
}
state = Immutable.fromJS(state);

const·item·=·Immutable.fromJS({column:·1});
const·nextState·=·state.updateIn(['sequence'],
                                     seq·=>·seq.push(item));