[Redux] Avoiding Array Mutations with concat(), slice(), and ...spread
For Redux, you cannot use mutable methods like push, splice. Need to use immutable methods such as concat, slice and ...spread
Html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="https://wzrd.in/standalone/expect@latest"></script>
<script src="https://wzrd.in/standalone/deep-freeze@latest"></script>
</head>
<body> </body>
</html>
push() vs concat()
push: modify the array;
concat: return a new array;
console.clear();
const addCounter = (list)=>{
list.push(0);
return list;
} const testAddCounter = ()=>{
const beforeList = [];
const afterList = [0]; deepFreeze(beforeList); expect(
addCounter(beforeList)
).toEqual(afterList);
} testAddCounter();
console.log("All tests passing.."); /**
"error"
"TypeError: Can't add property 0, object is not extensible
at addCounter (fegewebemu.js:5:8)
at testAddCounter (fegewebemu.js:15:10)
at fegewebemu.js:18:1"
*/ console.clear();
const addCounter = (list)=>{
let res = list.concat(0);
return res;
} const testAddCounter = ()=>{
const beforeList = [];
const afterList = [0]; deepFreeze(beforeList); expect(
addCounter(beforeList)
).toEqual(afterList);
} testAddCounter();
console.log("All tests passing.."); // passing
Using ...spread:
console.clear();
const addCounter = (list)=>{
return [...list, 0];
} const testAddCounter = ()=>{
const beforeList = [];
const afterList = [0]; deepFreeze(beforeList); expect(
addCounter(beforeList)
).toEqual(afterList);
} testAddCounter();
console.log("All tests passing.."); // Passing
splice() vs slice()
splice: modfiy array;
slices: return new array;
console.clear();
const removeCounter= (list, index)=>{
list.splice(index, 1);
return list;
} const testRemoveCounter = ()=>{
const beforeList = [0 ,10,20];
const afterList = [0, 20]; deepFreeze(beforeList); expect(
removeCounter(beforeList, 1)
).toEqual(afterList);
}
testRemoveCounter();
console.log("All tests passing.."); /**
"error"
"TypeError: Cannot add/remove sealed array elements
at removeCounter (fegewebemu.js:5:8)
at testRemoveCounter (fegewebemu.js:15:10)
at fegewebemu.js:17:1"
*/ console.clear();
const removeCounter= (list, index)=>{
return list.slice(0, index)
.concat(list.slice(index+1));
} const testRemoveCounter = ()=>{
const beforeList = [0 ,10,20];
const afterList = [0, 20]; deepFreeze(beforeList); expect(
removeCounter(beforeList, 1)
).toEqual(afterList);
}
testRemoveCounter();
console.log("All tests passing.."); // Passing
ES6:
console.clear();
const removeCounter= (list, index)=>{ return [
...list.slice(0, index),
....list.slice(index + 1)
];
} const testRemoveCounter = ()=>{
const beforeList = [0 ,10,20];
const afterList = [0, 20]; deepFreeze(beforeList); expect(
removeCounter(beforeList, 1)
).toEqual(afterList);
}
testRemoveCounter();
console.log("All tests passing.."); // Passing
Modify one element in array:
console.clear();
const incrementCounter = (list, index) => {
list[index]++; return list;
}; const testIncrementCounter = ()=>{
const beforeList = [0 ,10,20];
const afterList = [0, 11, 20]; deepFreeze(beforeList); expect(
incrementCounter(beforeList, 1)
).toEqual(afterList);
}
testIncrementCounter();
console.log("All tests passing..");
/**
"error"
"Error: Expected [ 0, 10, 20 ] to equal [ 0, 11, 20 ]
at Object.assert [as default] (https://wzrd.in/standalone/expect@latest:489:9)
at Expectation.toEqual (https://wzrd.in/standalone/expect@latest:70:26)
at testIncrementCounter (fegewebemu.js:16:43)
at fegewebemu.js:18:1"
*/ console.clear();
const incrementCounter = (list, index) => {
let res = list.slice(0, index)
.concat(++list[index])
.concat(list.slice(index+1)); return res;
}; const testIncrementCounter = ()=>{
const beforeList = [0 ,10,20];
const afterList = [0, 11, 20]; deepFreeze(beforeList); expect(
incrementCounter(beforeList, 1)
).toEqual(afterList);
}
testIncrementCounter();
console.log("All tests passing.."); // Passing
ES6:
console.clear();
const incrementCounter = (list, index) => {
return [
...list.slice(0, index),
++list[index],
...list.slice(index+1)
];
}; const testIncrementCounter = ()=>{
const beforeList = [0 ,10,20];
const afterList = [0, 11, 20]; deepFreeze(beforeList); expect(
incrementCounter(beforeList, 1)
).toEqual(afterList);
}
testIncrementCounter();
console.log("All tests passing.."); // Passing
[Redux] Avoiding Array Mutations with concat(), slice(), and ...spread的更多相关文章
- [Redux] Avoiding Object Mutations with Object.assign() and ...spread
Learn how to use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects. ...
- 数组方法concat & slice
掌握数组的操作方法: concat() / slice() concat() 语法: arrayObject.concat(arrayX,arrayY,....,arrayZ) 功能:用于连接两个或 ...
- 【译】Rust中的array、vector和slice
原文链接:https://hashrust.com/blog/arrays-vectors-and-slices-in-rust/ 原文标题:Arrays, vectors and slices in ...
- [Javascript] Array methods in depth - slice
Array slice creates a shallow copy of an array. In this lesson we cover, in detail, exactly what a ' ...
- JavaScript引用类型之Array数组的concat()和push()方法的区别
在javascript中,我们一般都只用push向数组的尾部插入新元素的,但是其实在javascript中还有另外一个方法和push一样,也是向数组尾部插入新元素的,但是他们之间却存在着一定的区别,当 ...
- (转)Array.prototype.slice.call自解
很多框架或者库里面都会有这句的使用,最多的还是通过Array.prototype.slice.call(arguments,0)把arguments这个伪数组转换为真正的数组.但为什么可以这么做,却一 ...
- 【Go入门教程2】内置基础类型(Boolean、数值、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
这小节我们将要介绍如何定义变量.常量.Go内置类型以及Go程序设计中的一些技巧. 定义变量 Go语言里面定义变量有多种方式. 使用var关键字是Go最基本的定义变量方式,与C语言不同的是Go把变量类型 ...
- 【GoLang】GoLang 遍历 map、slice、array方法
代码示例: map1 := make(map[string]string) map1["a"] = "AAA" map1["b"] = &q ...
- 详解 Array.prototype.slice.call(arguments)
首先,slice有两个用法,一个是String.slice,一个是Array.slice,第一个返回的是字符串,第二个返回的是数组 在这里我们看第二个方法 1.在JS里Array是一个类 slice是 ...
随机推荐
- Linux下登陆mysql服务器不需要输入账号密码信息
linux下登录mysql服务器一般都是在命令行手动输入链接信息 [root@localhost ~]# mysql -hlocalhost -uroot -p11111 而在mysql 5.6之后版 ...
- 集成支付宝后出现LaunchServices: ERROR: There is no registered handler for URL scheme alipay
原因如下: There's no problem with your implementation. All those warnings mean is the apps which each UR ...
- 三、singleton(单例化)一个对象的几种方法
方案一:私有化构造器,通过static final域 @Test public void test13() { A215 a=A215.a; A215 b=A215.a; System.out.pri ...
- PHP后台传值
前台数据往后台传值,往往是新手最头痛的,最近在学习thinkPHP的时候,也遇到了这种问题,总结一下,往不足之处请大家指教. 一.前台界面代码,往后台传值有两种方式,一种是get,另一种是post,新 ...
- cocos2dx lua调用C++类.
最近需求所迫, 终于着手传说中的 lua 了. 折腾了4天, 总算大概搞明白了用法. 细节咱们就别谈了, 直接说项目里怎么跑起来. 准备工作 我们需要一系列繁琐的前奏. tolua++: 这是必备工具 ...
- 关于auto和decltype
auto会忽略顶层const,保留底层const ; const int* const p = &i; auto p2 = p; //p2是const int*,不是const int* co ...
- Zsh安装CMake补全脚本进行CMake命令补全
最近在尝试使用Zsh,发现其补全命令的功能相当厉害.但对CMake命令的补全在默认的5.0.5中好像没有看到,网上找了下关于配置Zsh补全的文章也没有多少. 于是自己动手,发现在Zsh安装目录 ...
- (转载)用css来实现十字的布局
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 每日一算法【one】
//有一个数组 {1,2,3,4,5,6,7,8,9,12,13,45,67,89,99,101,111,123,134,565,677} 查找数组中是否有指定的某一个数. /** *------- ...
- [转] .NET 3.5中MSChart组件的ImageLocation属性含义
在.NET程序/网站中如果要生成统计图表/图形,以前可以采用OWC(Office Web Components),如OfficeXP组件OWC10.Office2003组件OWC11.OWC采用COM ...