[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 'shallow' copy is and how it can trip people up. We go on to look at examples that show to how to copy only the first item, the last item and even how to copy a sub-section of an array excluding the first and last. We end the lesson with a practical example that shows how slice
fits into a workflow that contains other array methods such as map
& reduce
.
It make a copy of array:
let ary = [1,2,3,4,5];
let copy = ary.slice(); console.log(copy); //[1, 2, 3, 4, 5]
Copy doesn't affect the old one if we push or modify one value (shallow copy)
let ary = [1,2,3,4,5];
let copy = ary.slice(); copy.push(6); console.log(ary); //[1, 2, 3, 4, 5]
console.log(copy); //[1, 2, 3, 4, 5, 6]
So what is shallow copy?:
Let's say inside the array, there is a reference to an object:
let person = {name: "Shane"};
let ary = [1,person];
let copy = ary.slice(); copy[1].name = "Kelly"; console.log(ary);
/*[1, [object Object] {
name: "Kelly"
}]
*/ console.log(copy);
/*
[1, [object Object] {
name: "Kelly"
}]
*/
If we modify the name prop of the object on the 'copy' array, it actually affect both array. So shadow copy -- if there is an referce of an object inside of array, it just copy the referece, not the actual object. That means, if change the object in one place, all the other places will change also.
More often use case is to take piece of array:
slice(startIndex, endIndex)
let ary = [1,2,3,4,5];
let copy1 = ary.slice(0,1);
let copy2 = ary.slice(2,3); console.log(copy1); //[1]
console.log(copy2); //[3]
If don't give the endIndex, it will take the rest of element:
let ary = [1,2,3,4,5];
let copy = ary.slice(2); console.log(copy); //[3, 4, 5]
let ary = [1,2,3,4,5];
let copy = ary.slice(-2); console.log(copy); //[4, 5]
let ary = [1,2,3,4,5];
let copy = ary.slice(1, -1); console.log(copy); //[2, 3, 4]
Example:
let person = {
name: 'Zhentian-Wan'
}; let filters = {
'desluglify': x => x.replace('-', ' '),
'uppercase': x => x.toUpperCase(),
'hello': x => "Hello, " + x + '!'
} let input = 'name | desluglify | uppercase | hello'; // ZHENTIAN WAN! let ary = input.split('|').map(x => x.trim()); let ref = person[ary[0]]; let res = ary.slice(1)
.reduce( (prev, next) => {
if(filters[next]){
return filters[next].call(null, prev);
}
}, ref); console.log(res);
[Javascript] Array methods in depth - slice的更多相关文章
- [Javascript ] Array methods in depth - sort
Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...
- [Javascript] Array methods in depth - filter
Array filter creates a new array with all elements that pass the test implemented by the provided fu ...
- [Javascript] JavaScript Array Methods in Depth - push
Array push is used to add elements to the end of an Array. In this lesson we'll see how the push met ...
- [Javascript] Array methods in depth - indexOf
indexOf is used to search for a value or reference inside of an array. In this lesson we first look ...
- [Javascript] Array methods in depth - some
some returns a boolean value after passing each item in the source array through the test function t ...
- JavaScript Array methods performance compare
JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...
- javascript Array Methods(学习笔记)
ECMAScript 5 定义了9个新的数组方法,分别为: 1.forEach(); 2.map(); 3.filter(); 4.every(); 5.some(); 6.reduce() ...
- JavaScript Array 对象
JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...
- JavaScript Array(数组)对象
一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...
随机推荐
- Android客户端采用Http 协议Post方式请求与服务端进行数据交互(转)
http://blog.csdn.net/javanian/article/details/8194265
- 解决java访问.netWebService的常见问题
到公司没多久,写了一个java调用.net写的webService结果期间用各种方法测试都没有完成,总是抛出异常,最后直接使用SOAP消息去进行调用才成功了,具体代码如下,仅供参考:import ja ...
- 关闭C#主窗体弹出是否关闭对话框
在开发系统时,常常有这样一个问题,就是当关闭主窗体,也即退出系统时,如果想提示是否关闭,以免误操作,可以在主窗体的Main_FormClosing事件中添加一个对话框,代码如下: private vo ...
- Windows Phone获得IsolatedStorage中指定目录下的所有文件
在Windows Phone 中对隔离存储空间中的文件操作需要通过System.Io.IsolatedStorage下的类进行操作 获得指定文件夹下的所有文件: 参数:是指定文件夹的路径加上通配符,格 ...
- HTML中插入视频
最常用的向HTML中插入视频的方法有两种,一种是古老的<object></object>标签,一种是html5中的<video></video>标签. ...
- 突然间,对JAVA也找到点感觉了。
书上没有那段代码,我自己修修补补弄完全了呢.... 就是感觉体系有点宏大,不要急,慢慢玩~!~~ 这个世界很精彩哟~~: QuizCard.java package QuizCard.sky.com; ...
- C51 延时程序
一.相关换算 1.1s=10^3ms(毫秒)=10^6μs(微秒)=10^9ns(纳秒)=10^12ps(皮秒)=10^15fs(飞秒)=10^18as(阿秒)=10^21zm(仄秒)=10^24ym ...
- 【HDOJ】4983 Goffi and GCD
题意说的非常清楚,即求满足gcd(n-a, n)*gcd(n-b, n) = n^k的(a, b)的不同对数.显然gcd(n-a, n)<=n, gcd(n-b, n)<=n.因此当n不为 ...
- MATLAB图像处理基础
MATLAB图像处理基础 2.2.1 图像文件格式及图像类型 1.MATLAB支持的几种图像文件格式: ⑴JPEG(Joint Photogyaphic Expeyts Group):一种称为联合图像 ...
- Subway POJ 2502
题目链接: http://poj.org/problem?id=2502 题目大意: 你刚从一个安静的小镇搬到一个吵闹的大城市,所以你不能再骑自行车去上学了,只能乘坐地铁或者步行去上学.因为你不想迟到 ...