重构 shift unshift every foreach some every filter
方法
shift
去除数组第一项
// Array.prototype.myshift = function(){
// if (this.length == 0) {
// return undefined;
// }
// var first = this[0]
// for(var i = 0; i < this.length;i++){
// this[i] = this[i + 1];
// }
// this.length = this.length - 1 ;
// return first ;
// }
// arr = [1,2,3,4]
// console.log( arr.myshift());
// console.log(arr);
unshift
数组前添加
// Array.prototype.myunshift = function(...arguments){
// oldlength = this.length ;
// this.length = this.length + arguments.length;
// for (let index = oldlength-1; index >= 0; index--) {
// this[index+arguments.length]=this[index]
// }
// for (let index = 0; index <arguments.length; index++) {
// this[index] = arguments[index];
// }
// return this.length;
// }
// arr=[1,2,3,4]
// console.log(arr.myunshift(6,7,8,1,2));
// console.log(arr);
every
测试一个数组内的所有元素是否都能通过指定函数的测试
// arr=[1,2,3,4]
// Array.prototype.myEvery = function(callback) {
// // 找不符合条件表达的数组元素直接返回false 其他返回true
// for (var i = 0; i < this.length; i++) {
// if(!callback(this[i])){
// return false;
// }
// }
// return true;
// }
// var res = arr.myEvery(function(item){
// console.log('every');
// return item>0
// });
// console.log(res);
foreach
对数组的每个元素执行一次给定的函数。
// var arr=[1,2,3,4,5]
// Array.prototype.myForEach = function(callback){
// for (var i = 0; i < this.length; i++) {
// callback(this[i],i,this)
// }
// }
// arr.myForEach(function(item,index,arr){
// console.log(item,index,arr);
// })
some
方法 只要有一项符合条件 返回true 否则返回false
// var arr=[1,2,3,4,5]
// Array.prototype.mySome = function(callback){
// for(var i = 0; i < this.length; i++){
// if((callback(this[i],i,this))){
// return true;
// }
// }
// return false;
// }
// var res = arr.mySome(function(item,index,arr){
// console.log('mysome');
// return item > 5
// })
// console.log(res);
filter
返回新数组,在原数组上判断是否满足条件来进行数据过滤,只会在原有数组上长度改变,元素内容不变
* 这段代码定义了一个 unique
函数,它接受一个数组作为参数,并返回一个新的数组,其中包含原数组中的不重复元素。
函数内部使用了 filter
方法来创建新数组。filter
方法接受一个回调函数作为参数,该回调函数对原数组中的每个元素进行测试。如果回调函数返回 true
,则该元素会被保留在新数组中;否则,该元素会被过滤掉。
在这个例子中,回调函数使用了箭头函数语法,并接受两个参数:item
和 index
。item
表示当前元素的值,index
表示当前元素的索引。
回调函数内部使用了 indexOf
方法来查找当前元素在原数组中第一次出现的位置。如果当前元素第一次出现的位置与当前索引相同,则说明这是一个不重复的元素,回调函数返回 true
;否则,说明这是一个重复的元素,回调函数返回 false
。
最终,filter
方法返回一个新数组,其中包含原数组中所有不重复的元素。
// function unique(arr) {
// return arr.filter((item, index) => arr.indexOf(item) === index);
// }
// arr = [1,2,1,2,53,4,7,3]
// console.log(unique(arr));
reduce
方法
这段代码定义了一个 unique
函数,它接受一个数组作为参数,并返回一个新的数组,其中包含原数组中的不重复元素。
函数内部使用了 reduce
方法来创建新数组。reduce
方法接受两个参数:一个回调函数和一个初始值。回调函数会对原数组中的每个元素进行处理,最终返回一个累积结果。初始值用于指定累积结果的初始值。
在这个例子中,回调函数使用了箭头函数语法,并接受两个参数:acc
和 cur
。acc
表示累积结果,cur
表示当前元素的值。
回调函数内部使用了三元运算符和 includes
方法来判断当前元素是否已经存在于累积结果中。如果当前元素已经存在于累积结果中,则返回累积结果;否则,使用扩展运算符将当前元素添加到累积结果中,并返回新的累积结果。
最终,reduce
方法返回一个新数组,其中包含原数组中所有不重复的元素。
[...acc, cur]
是一个数组字面量,它使用扩展运算符...
来将累积结果acc
中的所有元素添加到新数组中,然后再将当前元素cur
添加到新数组的末尾。
例如,如果累积结果acc
是[1, 2]
,当前元素cur
是3
,那么[...acc, cur]
的结果就是[1, 2, 3]
。
[]
是reduce
方法的第二个参数,它表示累积结果的初始值。在这个例子中,累积结果的初始值是一个空数组。
// function unique(arr) {
// return arr.reduce((acc, cur) => acc.includes(cur) ? acc : [...acc, cur], []);
// }
// arr = [1,2,1,2,53,4,7,3]
// console.log(unique(arr));