前言
提示:文中代码部分引用自:https://chinese.freecodecamp.org
一、map
- 介绍:
map() 方法创建一个新数组,其结果是该数组中的每个元素都调用一个提供的函数后返回的结果。
map() 不会改变原始数组。
- 调用:
array.map(function(curren,index,arr), thisValue)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
current:当前元素值
index:索引
arr:当前元素属于的数组对象
thisValue:没研究过,大佬们自己查资料吧
- 应用:
const users = [
{ name: 'John', age: 34 },
{ name: 'Amy', age: 20 },
{ name: 'camperCat', age: 10 }
];
const names = users.map(user => user.name);
console.log(names);
二、splice
- 介绍:(该方法会改变原始数组)
splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。
2.调用:
arrayObject.splice(index,howmany,item1,.....,itemX)
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
index: 从哪开始删除元素
howmany: 删除几个
item1: 新人报道 (旧的去了,要考虑新的要来)
3、应用:
let test0 = ["1","4","5","6"];
console.log(test0);//原数组
let test1 = test0.splice(0,1);
console.log(test0);//原数组数值已改变
console.log(test1);//返回被删除的数组
总结
循环渐进,每天学一些,当遇到难以理解的定义时,就想办法把它教给别人。