[Javascript] Creating an Immutable Object Graph with Immutable.js Map()

Learn how to create an Immutable.Map() through plain Javascript object construction and also via array tuples.

console.clear();
// Can be an object
var map = Immutable.Map({key: "value"});
console.log(map.get("key")); //"value"

// Can be an array
var map = Immutable.Map([["key", {"name": "Zhentian"}]]);
console.log(map.get("key").name); //"Zhentian"

// size prop
console.log(map.size); //1
mocha.setup(‘bdd‘);
const expect = chai.expect; 

function createObjTodos(numTodos) {

  var obj = {}
  _.each(_.range(numTodos), (index) => {
    const todoSequence = String(index+1);
    obj[‘todo‘+todoSequence] = {
      title: ‘Todo ‘+todoSequence,
      value: `Make ${todoSequence} happen`
    };
  });

  return obj;

} 

describe(‘Creating an Immutable Object Graph with Immutable.js Map()‘, () => {

  it(‘should create Map() with matching keys‘, () => {

    const data = {
      "todo1": {
        title: "Todo 1",
        value: "Make it happen"
      },
      "todo2": {
        title: "Todo 2",
        value: "Make it happen"
      }
    }

    let map = Immutable.Map(data);
    expect(map.get("todo1").title).to.equal("Todo 1");

  });

  it(‘should create Map() with keys from array tuples‘, () => {

    let map = Immutable.Map([["todo1", {title: "Todo 1"}]]) // Note the array within array
    expect(map.get("todo1").title).to.equal("Todo 1");

  });

  it(‘should create Map() with matching size to number of keys‘, () => {

    let map = Immutable.Map(createObjTodos(3))
    expect(map.size).to.equal(3);

  });

});

mocha.run();
时间: 2024-10-08 23:40:14

[Javascript] Creating an Immutable Object Graph with Immutable.js Map()的相关文章

Java多线程编程模式实战指南(二):Immutable Object模式--转载

本文由本人首次发布在infoq中文站上:http://www.infoq.com/cn/articles/java-multithreaded-programming-mode-immutable-object.转载请注明作者: 黄文海 出处:http://viscent.iteye.com. 多线程共享变量的情况下,为了保证数据一致性,往往需要对这些变量的访问进行加锁.而锁本身又会带来一些问题和开销.Immutable Object模式使得我们可以在不使用锁的情况下,既保证共享变量访问的线程安

iOS中 报错:mutating method sent to immutable object

今天我在切换账号登录,获取用户信息时,程序崩溃,报错:mutating method sent to immutable object 当我设置全局断点进行调试时,这个就是给我们所报错的方法,并打印报错信息 这是我们所调用的方法的地方,此时self.msgDict是不可变的字典,将它改为可变字典即可. 另外:mutating method sent to immutable object的意思就是说:要可变对象调用的方法,使用不可变对象调用

Java多线程编程模式实战指南(二):Immutable Object模式

多线程共享变量的情况下,为了保证数据一致性,往往需要对这些变量的访问进行加锁.而锁本身又会带来一些问题和开销.Immutable Object模式使得我们可以在不使用锁的情况下,既保证共享变量访问的线程安全,又能避免引入锁可能带来的问题和开销. Immutable Object模式简介 多线程环境中,一个对象常常会被多个线程共享.这种情况下,如果存在多个线程并发地修改该对象的状态或者一个线程读取该对象的状态而另外一个线程试图修改该对象的状态,我们不得不做一些同步访问控制以保证数据一致性.而这些同

iOS开发-mutating method sent to immutable object错误

今天干活的时候,遇到了这样一个问题.. 实在是太粗心了.mark下, 2014-01-05 11:44:34.762 softwareApp[1435:c07] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[JKDictionary setObject:forKey:]: mutating method sent to immutable objec

Immutable Object模式

多线程共享变量的情况下,为了保证数据一致性,往往需要对这些变量的访问进行加锁.而锁本身又会带来一些问题和开销.Immutable Object模式使得我们可以在不使用锁的情况下,既保证共享变量访问的线程安全,又能避免引入锁可能带来的问题和开销. Immutable Object模式简介 多线程环境中,一个对象常常会被多个线程共享.这种情况下,如果存在多个线程并发地修改该对象的状态或者一个线程读取该对象的状态而另外一个线程试图修改该对象的状态,我们不得不做一些同步访问控制以保证数据一致性.而这些同

[Immutable.js] Differences between the Immutable.js Map() and List()

The Immutable.js Map() is analogous to a Javascript Object or Hash since it is comprised of key-value pairs. The Immutable.js List() is analogous to a Javascript Array and contains many of the same native methods. Let's compare the two and dive into

[Javascript] bukld 'SQL' like object tree

Let's try creating a deeper tree structure. This time we have 4 separate arrays each containing lists, videos, boxarts, and bookmarks respectively. Each object has a parent id, indicating its parent. We want to build an array of list objects, each wi

JavaScript的数据类型都有什么? JavaScript中 toStirng() 与 Object.prototype.toString().call()

JavaScript的数据类型都有什么? (via  BAT互联网公司2014前端笔试面试题:JavaScript篇  http://www.sxt.cn/u/756/blog/4508) 基本数据类型:String,boolean,Number,Undefined, Null 引用数据类型: Object(Array,Date,RegExp,Function) 疑问:这些基本的数据类型的值都是常量,而常量是没有方法的,为什么能够调用方法呢?答案是这样的,五种基本类型除了null.undefin

[Immutable.js] Working with Subsets of an Immutable.js Map()

Immutable.js offers methods to break immutable structures into subsets much like Array--for instance it has the all powerful slice()--and unlike Array it offers functional methods like take() and skip(). You get the best of both the imperative and fu