简体   繁体   English

带有箭头函数回调的Array.prototype.filter()参数? (无此绑定)

[英]Array.prototype.filter() argument with arrow function callback? (no this binding)

Edit: This has been incorrectly marked as dupe (see discussion in comments) the solution to this specific problem is provided by article author here 编辑:这已被错误地标记为重复(请参阅注释中的讨论)本文作者在此处提供了针对此特定问题的解决方案

Inspired by https://hackernoon.com/rethinking-javascript-death-of-the-for-loop-c431564c84a8 I've been refactoring some old code. https://hackernoon.com/rethinking-javascript-death-of-the-for-loop-c431564c84a8的启发,我一直在重构一些旧代码。

Running into problems with passing arguments using Array.prototype.filter since the second parameter of Array.prototype.filter(callback, thisArg) binds the “this”-object in the callback but arrow functions don't bind “this”. 使用Array.prototype.filter传递参数时遇到问题,因为Array.prototype.filter(callback,thisArg)的第二个参数绑定了回调中的“ this”对象,但是箭头函数不绑定“ this”。

In my example I'm getting the keys from an associative array (yeah I know, technically not available in js) by using “Object.keys()”, then filtering that array by a property of their object in the associative array “this[item].property”, but that fails since this binding isn't available. 在我的示例中,我通过使用“ Object.keys()”从关联数组中获取键(是的,我知道,技术上在js中不可用),然后通过关联数组中其对象的属性过滤该数组。 [item] .property”,但由于此绑定不可用而失败。

So, embracing arrow functions, how does one pass parameters to callback in filter()? 因此,采用箭头功能,如何将参数传递给filter()中的回调?

 const arr = { a: { property: true, otherProp: false }, b: { property: true, otherProp: false }, }, hasProperty = item => this[item].property, getMatchingKeys = object => Object.keys(object).filter(hasProperty, object); getMatchingKeys(arr); 

You can use Object.entries . 您可以使用Object.entries It provides both the key and the value, so that you don't need the reference to the object itself: 它同时提供了键和值,因此您不需要引用该对象本身:

 const arr = { a: { property: true, otherProp: false }, b: { property: true, otherProp: false }, c: { property: false, // to be excluded otherProp: true }, }, hasProperty = ([key, value]) => value.property, first = ([key]) => key, getMatchingKeys = object => Object.entries(object).filter(hasProperty).map(first); console.log(getMatchingKeys(arr)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

You could also use bind -- not to bind this , but a first argument: 您还可以使用bind不是绑定this ,而是第一个参数:

 const arr = { a: { property: true, otherProp: false }, b: { property: true, otherProp: false }, c: { property: false, // to be excluded otherProp: true }, }, hasProperty = (object, key) => object[key].property, getMatchingKeys = object => Object.keys(object).filter(hasProperty.bind(null, arr)); console.log(getMatchingKeys(arr)); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

See also some other options in my answer to another question . 请参阅我对另一个问题的回答中的其他一些选项。

The author of the article provided an answer in the comments , provided here for reference: 本文的作者在评论中提供了答案,在此提供参考:

const arr = {
  a: {
    property: true,
    otherProp: false,
  },
  b: {
    property: true,
    otherProp: false,
  },
}
const hasProperty = object => item => object[item].property
const getMatchingKeys = object => Object.keys(object).filter(hasProperty(arr))
getMatchingKeys(arr) // = ['a', 'b']

Further reading, provided by @bergi in comments of original post (buried deep, posted here for greater visibility): @bergi在原始帖子的评论中提供的进一步阅读(深埋,在此处发布以提高可见度):

  1. jQuery pass more parameters into callback jQuery将更多参数传递给回调
  2. How do I pass an extra parameter to the callback function in Javascript .filter() method? 如何在Javascript .filter()方法中将额外的参数传递给回调函数?

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

相关问题 Array.prototype.filter() 期望在箭头函数 array-callback-return 结束时返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function array-callback-return Array.prototype.filter() 期望在箭头函数的末尾返回一个值 - Array.prototype.filter() expects a value to be returned at the end of arrow function react js Array.prototype.filter() 期望在箭头结束时返回一个值 function - react js Array.prototype.filter() expects a value to be returned at the end of arrow function 我返回 `boolean` 但 Array.prototype.filter() 期望在箭头 function 的末尾返回一个值 - i return `boolean` but Array.prototype.filter() expects a value to be returned at the end of arrow function Array.prototype.filter() 期望在函数 array-callback-return 结束时返回一个值 - Array.prototype.filter() expects a value to be returned at the end of function array-callback-return 原型方法上的JS Array.prototype.filter - JS Array.prototype.filter on prototype method 这个判断在Array.prototype.filter中是否重要 - Is this judgement essential in the Array.prototype.filter Javascript Array.prototype.filter意外输出 - Javascript Array.prototype.filter unexpected output Array.prototype.filter()返回空列表 - Array.prototype.filter() returns empty list 带有Javascript的高级Array.prototype.filter - Advanced Array.prototype.filter with Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM