官术网_书友最值得收藏!

  • Mastering Immutable.js
  • Adam Boduch
  • 147字
  • 2021-07-08 10:30:10

Pushing multiple list values

Now let's try pushing multiple values to a list:

const myList = List.of(1, 2, 3);
const myChangedList = myList
.push(4)
.push(5, 6)
.push(7, 8)
.push(9);

console.log('myList', myList.toJS());
// -> myList [ 1, 2, 3 ]
console.log('myChangedList', myChangedList);
// -> myChangedList List [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]

Here, the first three calls to push() create intermediary lists—you don't actually use them for anything other than as steps for building a new list. The final call to push() produces the value that ends up in myChangedList. You can see that push() accepts multiple values to add to the list. Of course, it would be better to make one call instead of four where possible. However, this isn't always possible depending on how your code is organized. I'm also using this as an opportunity to illustrate a concept.

主站蜘蛛池模板: 开远市| 宁阳县| 沅陵县| 比如县| 桐城市| 托克托县| 策勒县| 巨野县| 城市| 黑龙江省| 清远市| 谢通门县| 灯塔市| 海口市| 青河县| 惠安县| 五莲县| 徐闻县| 凯里市| 渝北区| 临夏县| 股票| 泗水县| 广昌县| 全南县| 阜康市| 辉南县| 韩城市| 永昌县| 葫芦岛市| 池州市| 调兵山市| 社会| 永平县| 汉源县| 阳谷县| 广饶县| 二连浩特市| 连州市| 如皋市| 乌拉特中旗|