[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 ...
随机推荐
- 创建一个流(Stream)可以让Bitmap或Image保存到流里面(转)
创建一个流(Stream)可以让Bitmap或Image使用save方法将已经在bitmap上生成的图像 保存到流里面?不需要直接在硬盘上生成文件 -------------------------- ...
- SQL Server主键自动生成_表and存储过程
主键表: CREATE TABLE [dbo].[KEYCODE]( [KeyName] [varchar](12) NOT NULL, [KeyTableName] [varchar](40) NU ...
- 创建dblink遇到一系列问题
创建dblink遇到一系列问题,有时间 把问题整理一下
- Swift - 06 - 数值类型转换和类型别名
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...
- StartCoroutine/StopCoroutineInvoke
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Unity_Coroutine.html using UnityEngine; us ...
- 浅谈C++ 异常处理的语义和性能
异常处理是个十分深奥的主题,这里只是浅论其对C++性能的影响. 在VC++中,有多个异常处理模式,三个最重要: No exception handling (无异常处理) C++ only (C++语 ...
- 跟我学android- 创建运行环境(二)
Android程序 需要在Android手机上运行,因此 Android开发时 必须准备相关运行,调试环境. Android真机(建议使用真机,速度快,效果好) Android虚拟设备(AVD) 使用 ...
- linux进程创建
1. 进程是程序的执行,会被加载到内存中,每个进程包括程序的代码和数据,其中数据包括程序的变量的数据,外部数据,程序堆栈. 2. Linux中,输入命令,如vi main.c 通过shell来执行, ...
- java面试题及答案(基础题122道,代码题19道)
JAVA相关基础知识 1.面向对象的特征有哪些方面 1.抽象: 抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部问题,而只是选择其中的一部分, ...
- CentOS7 开启关闭网卡
ifdown ifcfg-enp7s0 关闭网卡 ifup ifcfg-enp7s0 开启网卡