简体   繁体   English

"这个 Array.prototype.find() 是如何工作的?"

[英]How does this Array.prototype.find() work?

What I do not understand is the function and its parameter fruit , because it's not being passed anything.我不明白的是函数及其参数fruit ,因为它没有被传递任何东西。

var inventory = [
  {name: 'apples', quantity: 2},
  {name: 'bananas', quantity: 0},
  {name: 'cherries', quantity: 5}
];

function findCherries(fruit) { 
  return fruit.name === 'cherries';
}

console.log(inventory.find(findCherries));

...because it's not being passed anything. ...因为它没有被传递任何东西。

It is being passed something, you just don't see the code doing it, which is inside the Array.prototype.find method's code. 传递的东西,你只是看不到代码这样做,这是内部 Array.prototype.find方法的代码。 Remember, this line: 记住,这一行:

console.log(inventory.find(findCherries));

...passes the value of findCherries (a function reference) into find , it doesn't call findCherries . ...将findCherries (函数引用)的传递给find ,它不调用 findCherries find does that, once for each entry in the array, supplying three arguments (only one of which findCherries uses). find会为数组中的每个条目一次提供三个参数( findCherries仅使用其中一个)。 The first of those arguments is the array entry for that call. 这些参数中的第一个是该调用的数组条目。

The full polyfill for Array.prototype.find makes for tricky reading, so let's do something similar but simpler so you can see the calls: Array.prototype.find的完整Array.prototype.find使得Array.prototype.find棘手,所以让我们做一些类似但更简单的事情,以便您看到调用:

// A bit like Array.prototype.find, but **not** the same; much simplified
function fakeFind(callback) {
  for (var index = 0; index < this.length; ++index) {
    var entry = this[index];
    //  vvvvvvvvvvvvvvvvvvvvvvvvvvvv---- This is where findCherries is called
    if (callback(entry, index, this)) {
      return entry;
    }
  }
  return undefined;
}

Live copy: 实时复制:

 // A bit like Array.prototype.find, but **not** the same; much simplified function fakeFind(callback) { for (var index = 0; index < this.length; ++index) { var entry = this[index]; // vvvvvvvvvvvvvvvvvvvvvvvvvvvv---- This is where findCherries is called if (callback(entry, index, this)) { return entry; } } return undefined; } Object.defineProperty(Array.prototype, "fakeFind", { value: fakeFind }); var inventory = [ {name: 'apples', quantity: 2}, {name: 'bananas', quantity: 0}, {name: 'cherries', quantity: 5} ]; function findCherries(fruit) { return fruit.name === 'cherries'; } console.log(inventory.fakeFind(findCherries)); 

If you expand the function so that it uses an anonymous function, it kind of makes more sense.. 如果扩展该函数以使其使用匿名函数,则这更有意义。

If you look at the code snippet below, it kind of explains it a bit more.. 如果您查看下面的代码段,它会稍微说明一下。

 var inventory = [ {name: 'apples', quantity: 2}, {name: 'bananas', quantity: 0}, {name: 'cherries', quantity: 5} ]; function findCherries(fruit) { return fruit.name === 'cherries'; } //this kind of does the same, apart from the extra anonymous function //but here you can see this `item` that's getting passed console.log(inventory.find( function (item) { return findCherries(item); } )); //because in the above the anonymous function has the same //signature, it's made redunant, so we may as well //call findCherries direct.. console.log(inventory.find(findCherries)); 

The find() method returns a value in the array, if an element in the array satisfies the provided testing function. 如果数组中的元素满足提供的测试功能,则find()方法将在数组中返回一个值。 Otherwise undefined is returned. 否则返回undefined。 Array.prototype.find() Array.prototype.find()

In your code snippet fruit is an item from your original array, i its value is an item in your array. 在您的代码段中, fruit是原始数组中的一项,即它的值是数组中的一项。 findCherries will be compare each element in your array for a match with property name (this is the provided testing function). findCherries将比较数组中的每个元素是否与属性name匹配(这是提供的测试功能)。

Your findCherries callback will be executed on each value in the array. 您的findCherries回调将在数组中的每个值上执行。 When a match is found a result is returned. 找到匹配项后,将返回结果。

Implementation of Array find method.数组查找方法的实现。

 Array.prototype.MyFind = function(callback) { for (let i = 0; i < this.length; i++) { if(callback(this[i])){ return this[i]; } } } const array1 = [5, 12, 8, 130, 44]; const found = array1.find(element => element > 10 & element > 30 & element < 50); console.log(found);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM