[英]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在原始帖子的评论中提供的进一步阅读(深埋,在此处发布以提高可见度):
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.