Giter Club home page Giter Club logo

arraymethod's Introduction

forEach()方法的实现

forEach():无返回值,单纯的遍历数组。

Array.prototype.myForEach = function(cb){ // 在Array对象的原型上添加 myForEach()方法, 接受一个回调函数
	for(let i=0; i<this.length; i++){ // this指的是当前实例化的数组对象
		let item = this[i]; // 定义回调函数接受的三个参数
		let index = i;
		let array = this;
		cb(item,index,array) // 调用
	}
}

map()方法的实现

map():有返回值,无论返回什么都添加到新数组中。

Array.prototype.myMap = function(cb) {
	let newArr = []; // 定义一个新的数组,用来接受返回值
	for (let i = 0; i < this.length; i++) {
		let item = this[i];
		let index = i;
		let array = this;
		let result = cb(item, index, array);
		newArr.push(result) // 将回到函数的结果push到新的数组中
	}
	return newArr // 返回这个新的数组
}

filter()方法的实现

filter(): 有返回值,将返回结果为true的每一项push到一个新的数组中。

Array.prototype.myFilter = function(cb) {  // 实现方法和map()方法差不多
	let newArr = [];
	for (var i = 0; i < this.length; i++) {
		let item = this[i];
		let index = i;
		let array = this;
		let result = cb(item, index, array); 
		if (result) { // 判断回调函数的结果否为true,为true则将 该项的item push到新的数组中
			newArr.push(item);
		}
	}
	return newArr; // 返回新的数组
}

every()方法的实现

every(): 有返回值,如果数组中的每一项都通过了测试,则返回true;反之返回false.

Array.prototype.myEvery = function(cb){
	for (var i = 0; i < this.length; i++) {
		let item = this[i];
		let index = i;
		let array = this;
		let result = cb(item, index, array);
		if (!result) { // 当返回false时,怎停止遍历,返回false。
			return false; 
		}
	}
	return true;
}

some()方法的实现

Array.prototype.mySome = function(cb) {
	for (var i = 0; i < this.length; i++) {
		let item = this[i];
		let index = i;
		let array = this;
		let result = cb(item, index, array);
		if (result) {
			return true; // 有一个通过了测试 则终止遍历,返回true
		}
	}
	return false; // 一个都没有通过测试,返回false
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.