世间自有公道,付出总有回报,说到不如做到,要做就做好,步步高!

【译】Immutable.js : 操作LIST 3

我们已经了解了如何创建一个新列表,但是如何获取它的数据,或者在创建后添加或删除数据?这并不总是显而易见的,所以这里有一些您希望在文档中看到的例子。

Getters

get()

像数组一样,索引从0开始

// Get a value from a List

const avengersList = Immutable.List(['ironMan' , 'captainAmerica']);

// Get captainAmerica
avengersList.get(1);

get(-1) 获取最后一个

如果通过负数取值,从列表中最后一个开始返回(-1最后一个,-2倒数最二个,依次类推)

// Get a value from a List

const avengersList = Immutable.List(
  ['ironMan' , 'captainAmerica', 'blackWidow', 'theHulk']);

// Get blackWidow
avengersList.get(-2);

getIn() 获取嵌套结构中的数据

getin()函数将允许您访问一个深度嵌套的列表列表。您提供了一个keyPath——一个索引数组——进入嵌套的列表中,告诉不可变的您想要访问的值的位置。keyPath只是用来访问嵌套的ES6数组的同一组索引,而不是使用ES6数组索引语法(例如index[1][1][1][0]),而是使用一系列的索引(例如,[1,1,1,0])。

// Get a value from a deeply nested List
const avengers = [
  'ironMan', // index [0]
  ['captainAmerica', // index [1][0]
   ['blackWidow', // index [1][1][0]
    ['theHulk'] // index [1][1][1][0]
   ]
  ]
];

const avengersList = Immutable.fromJS(avengers);

// get theHulk
avengersList.getIn([1, 1, 1, 0]);

Setters

有了setter,您必须始终记住您在处理的是 不可变数据;也就是说,即使一个函数可以让你在列表中设置一个值,你实际上是在列表的一个新副本中设置了值——被执行的列表完全没有改变。

因此,当您看到‘replace’, ‘delete’, ‘update’,等字样时,请务必记住原始列表不会被更改 - 它将返回包含更改的副本。

List.set()

使用set()方法覆盖现有的项

//Replace a value in a List with set()
​
const avengersList = Immutable.List(['ironMan' , 'captainAmerica']);
​
// change ironMan to blackWidow
avengersList.set(0, 'blackWidow');

使用set()替换列表中的倒数第二个值

负指数从列表的末尾开始计算,最后一个值:-1,所以倒数第二个是 -2。

list.set(-2, newValue)

// Replace the second-to-last value in a List with set()

const avengersList = Immutable.List(
  ['ironMan' , 'captainAmerica', 'blackWidow', 'theHulk', 'antMan']);

// Replace theHulk with scarletWitch (sorry Bruce)
avengersList.set(-2, 'scarletWitch');

使用set()将值添加到超出当前列表大小的索引处

如果索引超出列表的大小,则列表将增长到新的索引,其间的索引将填充为null

// Add a value to a List at an index beyond its current size

const avengersList = Immutable.List(['ironMan' , 'captainAmerica']);

// Add antMan to a place no normal human would fit
avengersList.set(8, 'antMan');

使用set()将新值添加到列表的末尾

由于使用set()的索引大于List的当前大小,因此在该位置创建一个新项目,指定等于List的当前大小的索引将在List的末尾添加一个项目,但不会插入空值。

list.set(indexOfLastValuePlus1, newValue)
// Note: you can get indexOfLastValuePlus1 using list.size

// Add a new value to the end of a List with set()
​
const avengersList = Immutable.List(['ironMan' , 'captainAmerica']);
​
// Add blackWidow to the List
avengersList.set(avengersList.size, 'blackWidow');

List.setIn()

setin()函数将允许您访问一个深度嵌套的列表列表。同样的规则适用于list . set()(例如,从列表末尾开始的负值索引,>大小的索引将扩展列表的大小,等等)。与List . getin()一样,您提供了一个keyPath——数组的索引——进入嵌套列表,告诉setInI()您想要更改的值的位置。

用setIn()替换深层次的List列表中的值

//Replace a value in a nested List with setIn()

const avengers = [
  'ironMan',           // index [0]
  ['captainAmerica',   // index [1][0]
   ['blackWidow', // index [1][1][0]
    ['theHulk']    // index [1][1][1][0]
   ]
  ]
];

const avengersList = Immutable.fromJS(avengers);

// change theHulk to scarletWitch
avengersList.setIn([1, 1, 1, 0], 'scarletWitch');

使用setIn()将新值添加到嵌套列表的末尾

// Add a new value to the end of a nested List
const avengers = [
  'ironMan', // index [0]
  ['captainAmerica', // index [1][0]
   ['blackWidow', // index [1][1][0]
    ['theHulk'] // index [1][1][1][0]
   ]
  ]
];

const avengersList = Immutable.fromJS(avengers);

// Add scarletWitch to theHulk's List
avengersList.setIn([1, 1, 1, 1], 'scarletWitch');

使用setIn()替换嵌套列表中的倒数第二个值

// Replace the second-to-last value in a nested List with setIn()

const avengers = [
  'ironMan', // index [0]
  ['captainAmerica', // index [1][0]
   ['blackWidow', // index [1][1][0]
    ['theHulk', 'scarletWitch'] // index [1][1][1][0]
   ]
  ]
];

const avengersList = Immutable.fromJS(avengers);

// Replace theHulk with vision (sorry Bruce)
avengersList.setIn([1, 1, 1, -2], 'vision');

如果索引不存在会发生什么?

如果keyPath中的任何位置都不存在索引,则List.setin()将执行以下操作之一:

  • 如果指定的索引在列表不存,,那么此索引将尝试在该列表的边界之外的地方插入一个值。通常的规则应用,并且列表展开,值被放置在扩展列表的末尾,并在中间插入null。
  • 如果索引前面的索引是一个值(即不是列表或映射),那么糟糕的索引试图将值插入到没有索引概念的数据类型中;因此,将抛出一个无效的keyPath错误。
  • 如果在keyPath中添加了太多的索引,那么错误的索引就是试图将一个值插入到一个根本不存在的List中。在这种情况下,将会创建一个新的Map,其中错误的索引是关键。

如果您在试图解决发生的问题时遇到问题,请记住,在错误索引之前,行为取决于索引引用的数据类型。

//Replace a value in a nested List with setIn()

const avengers = [
  'ironMan',  			// index [0]
  ['captainAmerica', 	// index [1][0]
   ['blackWidow', 		// index [1][1][0]
    ['theHulk']		// index [1][1][1][0]
   ]
  ]
];

const avengersList = Immutable.fromJS(avengers);

// change theHulk to scarletWitch
avengersList.setIn([1, 1, 1, 0], 'scarletWitch');

Item Adders and Inserters

使用insert()将项目添加到列表的前面

const newList = list.insert(0, newValue)

// Add an item to the front of a List

const avengersList = new Immutable.List(['ironMan', 'captainAmerica']);

// Add blackWidow
avengersList.insert(0, 'blackWidow');

使用unshift()将项目添加到列表的前面

const newList = list.unshift(newValue)

// Add an item to the front of a List
​
const avengersList = new Immutable.List(['ironMan', 'captainAmerica']);
​
// Add blackWidow
avengersList.unshift('blackWidow');

使用insert()插入到列表中的指定位置

const newList = list.insert(index, newValue)

// Insert an item within a List

const avengersList = new Immutable.List(['ironMan', 'captainAmerica']);

// Insert blackWidow between ironMan and captainAmerica
avengersList.insert(1, 'blackWidow');

使用insert()将一个项目追加到列表的末尾

const newList = list.insert(list.size, newValue)

// Append an item to the end of a List with insert

const avengersList = new Immutable.List(['ironMan', 'captainAmerica']);

// append blackWidow
avengersList.insert(avengersList.size, 'blackWidow');

用push()将一个项目追加到列表的末尾

const newList = list.push(list.size, newValue)

// Push an item onto the end of a List

const avengersList = new Immutable.List(['ironMan', 'captainAmerica']);

// append blackWidow
avengersList.push('blackWidow');

Deleters

使用delete()从列表的前面删除项目

const newList = list.delete(0)

// Delete an item from the front of a List

const avengersList = new Immutable.List(['ironMan', 'captainAmerica', 'blackWidow']);

// 'bye Tony
avengersList.delete(0);

使用delete()删除列表中的指定的项

const newList = list.delete(index)

// Delete an item within a List

const avengersList = new Immutable.List(['ironMan', 'captainAmerica', 'blackWidow']);

// so long, 'cap
avengersList.delete(1);

使用delete()从列表的末尾删除项目

const newList = list.delete(list.size - 1)

// Delete an item from the end of a List

const avengersList = new Immutable.List(['ironMan', 'captainAmerica', 'blackWidow']);

// see ya later, Natasha
avengersList.delete(avengersList.size - 1);

使用pop()从列表的末尾删除项目

const newList = list.pop()

你可能会认为List.pop()从List中删除最后一个项目,就像JavaScript本地的Array.pop()一样.然而,Array.pop()会从数组本身中删除项目(从而改变它),并返回已被删除的项目;相比之下,Immutable的List.pop()返回一个新的List,其中的项目被删除,保持现有的List不变。

因此,您无法获取使用List.pop()移除的项目的值

如果您确实需要这个值,请使用List.last()显式查询原始List(您可以在调用List.pop()之前或之后执行此操作,因为原始List没有变化 - 这就是Immutable库)。

// Pop an item from the end of a List
​
const avengersList = new Immutable.List(['ironMan', 'captainAmerica', 'blackWidow']);
​
// see ya later, Natasha
avengersList.pop();

用delete()删除列表的倒数第二项

负指数从列表的末尾开始计算。最后一个值= -1,所以倒数第二个= -2。

const newList = list.delete(-2)

// Delete the second-to-last item from a List

const avengersList = new Immutable.List(['ironMan', 'captainAmerica', 'antMan', 'blackWidow']);

// where's antMan gone?
avengersList.delete(-2);

清除列表中的所有值

// Delete the second-to-last item from a List

const avengersList = new Immutable.List(['ironMan', 'captainAmerica', 'blackWidow', 'antMan']);

// kill all Avengers
avengersList.clear();

Merging Lists

现在您已经知道如何添加和删除列表中的项目了,现在是时候进一步学习如何合并两个或多个Immutable.js列表。这就是Immutable快速超越JavaScript的数组,使用专门的函数将其变成强大的数据处理工具。

posted @ 2017-12-07 16:28  疯狂秀才  阅读(1336)  评论(0编辑  收藏  举报