1. Features of two functions:

1. The find function is mainly used to find an element item that meets the judgment conditions and return it. After the function is executed, the original array is not affected and a new space is opened for storing elements.

2. The findIndex function mainly returns the subscript index value of the item element that meets the conditions. It also does not change the original array and opens up a new space to store the subscript value.

1. Find function source

Array.prototype.myFind = function(returnItemFunc){array.prototype. myFind = function(returnItemFunc){ This refers to the array in which the function is called for(let I = 0; i<this.length; I++){// returnItemFunc is actually a callback function. // When the callback returns true, the most important thing is to return the corresponding element. We said above that this refers to the array in which the callback was called, and namethid[I] refers to the element in the array with the index I. Arr. Find (function(item,index){return.... }) if(returnItemFunc(this[I], I)){return this[I]}}} var arr = [1,2,3,4] var res = arr.myfind (function(item,index){ return item === 3 }) console.log(res) // 3Copy the code

FindIndex function source code

//findIndex returns the index of the element that meets the condition true. / / the source code, just return the index subscript Array. The prototype. MyfindIndex = function (returnItemIndexFunc) {for (let I = 0; i<this.length; i++){ if(returnItemIndexFunc(this[i],i)){ return i } } } var res2 = arr.myfindIndex(function(item,index){ return item === 1 }) console.log(res2) // 0Copy the code