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的更多相关文章

  1. [Javascript ] Array methods in depth - sort

    Sort can automatically arrange items in an array. In this lesson we look at the basics including how ...

  2. [Javascript] Array methods in depth - filter

    Array filter creates a new array with all elements that pass the test implemented by the provided fu ...

  3. [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 ...

  4. [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 ...

  5. [Javascript] Array methods in depth - some

    some returns a boolean value after passing each item in the source array through the test function t ...

  6. JavaScript Array methods performance compare

    JavaScript Array methods performance compare JavaScript数组方法的性能对比 env $ node -v # v12.18.0 push vs un ...

  7. javascript Array Methods(学习笔记)

    ECMAScript 5 定义了9个新的数组方法,分别为: 1.forEach();  2.map();  3.filter();  4.every();  5.some();  6.reduce() ...

  8. JavaScript Array 对象

    JavaScript Array 对象 Array 对象 Array 对象用于在变量中存储多个值: var cars = ["Saab", "Volvo", & ...

  9. JavaScript Array(数组)对象

    一,定义数组 数组对象用来在单独的变量名中存储一系列的值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, elem ...

随机推荐

  1. Android打开系统的Document文档图片选择

    打开Document UI 过滤图片 private void startAcitivty() { Intent intent = new Intent(); intent.setAction(&qu ...

  2. WPF 弱事件

    因为在接触WPF的过程中追查INotifyPropertyChanged的通知原理的时候,发现了 PropertyChangedEventManager这个类,它是继承与WeakEventManage ...

  3. 03C#基础(2)

    1.比较运算符 ==等于;  !=不等于;  >大于;  >=大于或者等于;  <小于;  <=小于或者等于; 比较运算符(又称关系运算符)用来进行值得真假性判断,结果是boo ...

  4. MYSQL :逗号分隔串表,分解成竖表

    DROP TEMPORARY TABLE IF EXISTS Temp_Num ; CREATE TEMPORARY TABLE Temp_Num ( xh INT PRIMARY KEY ); -- ...

  5. ERROR:the server has either erred or is incapable of performing the requested operation

    openstack中,有时会经常出现这种错误,原因无二,一是安全组没有设置正确,二是openstack中网络配置会有些问题或者是相关的服务没有启动. 解决方法:1.安全组问题在nova.conf和ne ...

  6. SxsTrace工具使用方法(转)

    http://blog.sina.com.cn/s/blog_494e45fe0102dtt3.html Windows7平台上有一个强大的SxsTrace工具,可以跟踪调试应用程序运行时需要的动态库 ...

  7. Java学习----方法的重载

    一个类中有多个同名的参数不一样的方法. 作用:可以根据不同的条件调用不同的方法. 注意:java不会因为方法的返回类型或者权限的不同而判断为不同的两个方法. public class Student ...

  8. C语言结构体占用空间内存大小解析

    结构体的数据类型的有点我们就不啰嗦了,直接来看相同数据结构体的几种书写的格式吧. 格式一: 01.struct tagPhone 02.{ 03.     char   A; 04.     int  ...

  9. [转载] $\mathrm{Jordan}$标准型的介绍

    本文转载自陈洪葛的博客$,$ 而实际上来自xida博客朝花夕拾$,$ 可惜该博客已经失效 $\mathrm{Jordan}$ 标准形定理是线性代数中的基本定理$,$ 专门为它写一篇长文好像有点多余$: ...

  10. .net反射详解

    前言 之所以要写这篇关于C#反射的随笔,起因有两个:   第一个是自己开发的网站需要用到   其次就是没看到这方面比较好的文章. 所以下定决心自己写一篇,废话不多说开始进入正题. 前期准备 在VS20 ...