[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 ...
随机推荐
- MySQL拷贝表的几种方式
假如我们有以下这样一个表: id username password ----------------------------------- 1 admin * ...
- ASP.NET菜鸟之路之实现新闻列表增删改
背景 我是一个ASP.NET菜鸟,暂时开始学习ASP.NET,在此记录下我个人敲的代码,没有多少参考价值,请看到的盆友们为我点个赞支持我一下,多谢了. 网站介绍 根据视频的例子修改的方法,其中数据不经 ...
- javascript 操作符类型隐性转换
javascript 操作符类型隐性转换 (一).一元操作符只能操作一个值的操作符叫做一元操作符1.递增和递减操作符a. 在应用于一个包含有效数字字符的字符串时,先将其转换为数字值,再执行加减1的操作 ...
- XML约束
XML约束--能够看懂约束内容,根据约束内容写出符合规则的xml文件. DTD约束 1)导入dtd方式 内部导入 <!DOCTYPE note [ <!ELEMENT note (to,f ...
- MESH
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_meshtest.html A class that allows c ...
- 【USACO 2.1.3】三值的排序
[题目描述] 排序是一种很频繁的计算任务.现在考虑最多只有三值的排序问题.一个实际的例子是,当我们给某项竞赛的优胜者按金银铜牌排序的时候.在这个任务中可能的值只有三种1,2和3.我们用交换的方法把他排 ...
- 妙用git rebase --onto指令
有时候,在分支提交更改的时候,会忘记rebase,就直接提交上去,或者忘记和本地远程分支做merge,就直接rebase了别的分支.有时候真希望有一种切片的方式,让自己的分支只需要接上某一段.这个时候 ...
- 如何去除 ckeditor 上传图片后在原码中留下的 style="width: 100%;height:100px"之类的代码呢?
ckeditor编辑器在上传图片的时候,会神奇的加上一段诡异的代码: 这导致上传的小图也是被拉伸到100%,我根本就没有定义它,找来找去也找不到element.style,原来这是在system.cs ...
- 字符串截取slice() substring() substr()的区别?
获取子字符串 slice() substr() substring() 不会修改字符串本身,他们只是返回一个基本类型的字符串值 var str='abcdefghijklmn'; ...
- PHP面向对象(OOP)编程完全教程:10.__set(),__get(),__isset(),__unset()四个方法的应用
一般来说,总是把类的属性定义为private,这更符合现实的逻辑.但是, 对属性的读取和赋值操作是非常频繁的,因此在PHP5中,预定义了两个函数”__get()”和”__set()”来获取和赋值其属性 ...