博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Javascript] Array methods in depth - slice
阅读量:6623 次
发布时间:2019-06-25

本文共 2342 字,大约阅读时间需要 7 分钟。

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);

 

转载地址:http://iijpo.baihongyu.com/

你可能感兴趣的文章
(转)android 牛人必修 ant 编译android工程
查看>>
求最大公约数与最小公倍数
查看>>
C# Winform 跨线程更新UI控件常用方法总结(转)
查看>>
eclipse菜单栏不显示 + the system is running in lou-graphics mode问题
查看>>
【WebService】使用jaxb完成对象和xml的转换
查看>>
如何去除My97 DatePicker控件上右键弹出官网的链接 - 如何debug混淆过的代码
查看>>
CAAnimation动画
查看>>
弹出框插件集合
查看>>
css 固定表头的表格,和 width:auto, margin:auto等 自计算方法
查看>>
身份证号验证
查看>>
新手应该知道的流量概念
查看>>
16、约瑟夫问题
查看>>
R 安装car包失败
查看>>
软工网络15Alpha阶段敏捷冲刺博客汇总
查看>>
仿写百度首页
查看>>
今日词话:点绛唇·感兴
查看>>
iOS面试题(二)
查看>>
UVA116 Unidirectional TSP 单向TSP
查看>>
React 新手随笔
查看>>
阿里云手动安装特定版本的nginx
查看>>