[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 ...
随机推荐
- this,super关键字的使用
this关键字 1.this是对象的别名,是当前类的实例引用 2.在类的成员方法内部使用,代替当前类的实例.在Java中,本质上是指针,相当于C++中的指针概念.如果方法中的成员在调用前没有操作实例名 ...
- mssql的holdlock锁跟索引的关系
表锁tablock是会给表所有数据附加共享锁,但是只是当前语句有效,语句执行完毕,锁释放,而不会持续到事务结束,而tablockX表锁是持续到事务结束的锁 holdlock锁,锁定的范围会根据wher ...
- POJ 1240 Pre-Post-erous! 解题报告
题意: 给出一个m叉树的前,后序遍历求这样的树有多少种. Solution: 我们知道前序遍历的第一个点一定是根节点,后序遍历的最后一个点一定是根节点. 由此,我们只一要确定对于每一个节点,它有多少个 ...
- 【BZOJ1010】玩具装箱
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- 66 Plus One(大数+1Easy)
题目意思:vector<int> v存数 eg.123 则v[0]=1,v[1]=2,v[2]=3,加1后返回一个vector 思路:先判断新的vector长度是否需要加1,然后从v ...
- PHP 运算符 详解
PHP 算数运算符 运算符 名称 例子 结果 + 加法 $x + $y $x 与 $y 求和 - 减法 $x - $y $x 与 $y 的差数 * 乘法 $x * $y $x 与 $y 的乘积 / 除 ...
- [Python笔记]第五篇:递归
本篇主要内容:递归以及冒泡排序 参考文章:(http://www.cnblogs.com/balian/archive/2011/02/11/1951054.html) 递归的概念 递归的概念很简单, ...
- Python学习笔记:04函数
Python 函数 通过分而治之的方法解决问题是一种很自然的思路.函数就是将解决特定问题的方法进行抽象. def fibs(num): 'calculate the first num th fib ...
- postgres常用类型
数值类型 名字 存储空间 描述 范围 smallint 2 字节 小范围整数 -32768 到 +32767 integer 4 字节 常用的整数 -2147483648 到 +2147483647 ...
- testservice小项目总结
关于自做小项目testservice的总结: 1.Activity与Service的绑定及之间的通信: 1)关于Activity和Service的生命周期的理解: 2)bindService方法中Se ...