• Immutable.List

    Immutable.List

    List是JavaScript数组结构的不可变版本。

    1. let movies = Immutable.fromJS([ // again use fromJS for deep immutability
    2. {
    3. name: 'The Fellowship of the Ring',
    4. released: 2001,
    5. rating: 8.8
    6. },
    7. {
    8. name: 'The Two Towers',
    9. released: 2002,
    10. rating: 8.7
    11. }
    12. ]);
    13. movies = movies.push(Immutable.Map({
    14. name: 'The Return of the King',
    15. released: 2003
    16. }));
    17. movies = movies.update(2, movie => movie.set('rating', 8.9)); // 0 based
    18. movies = movies.zipWith(
    19. (movie, seriesNumber) => movie.set('episode', seriesNumber),
    20. Immutable.Range(1, movies.size + 1) // size property instead of length
    21. );
    22. console.log(movies);
    23. /* writes
    24. List [
    25. Map { "name": "The Fellowship of the Ring", "released": 2001, "rating": 8.8, "episode": 1 },
    26. Map { "name": "The Two Towers", "released": 2002, "rating": 8.7, "episode": 2 },
    27. Map { "name": "The Return of the King", "released": 2003, "rating": 8.9, "episode": 3 } ]
    28. */

    这里我们再次使用Immutable.fromJS调用,因为我们有对象存储在数组中。 我们调用push来向列表中添加项目,就像我们在数组上调用它一样。 但是因为我们创建了一个新的副本,我们必须重新绑定变量。 当我们想更新特定索引中的项目时,我们有相同的 setupdate调用。 我们还可以访问数组函数,如mapreduce还支持像我们这里使用的额外方法zipWith