2017-08-31 35 views
0

編輯:這已被錯誤地標記爲重複數據刪除技術(見註釋討論)解決這一特定問題是由文章作者here帶有箭頭函數回調的Array.prototype.filter()參數? (沒有這種綁定)

https://hackernoon.com/rethinking-javascript-death-of-the-for-loop-c431564c84a8提供啓發我已經重構了一些舊碼。

由於Array.prototype.filter(callback,thisArg)的第二個參數在回調中綁定了「this」對象,但箭頭函數不綁定「this 」。

在我的示例中,我通過使用「Object.keys()」從關聯數組中獲取關鍵字(是的,我知道,技術上在js中不可用),然後通過其對象的屬性關聯數組「this [item] .property」,但由於此綁定不可用而失敗。

因此,擁抱箭頭函數,如何將參數傳遞給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);

+1

箭頭函數不綁定到此上自己https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions – TimCodes

+2

爲什麼它需要首先涉及'this'? '.filter(v => arr [v] .property)'已經非常可讀和簡短了。 – loganfsmyth

+0

'hasProperty'和'getMatchingKeys'是否應該在'arr'對象字面值內? – Barmar

回答

1

您可以使用Object.entries。它同時提供鍵和值,這樣你就不需要參考對象本身:

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; }

你也可以使用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; }

另請參閱my answer to another question中的其他選項。

+0

我無法得到這個工作?對我來說,它產生與使用鍵相同的數組? – Hessius

+0

它不會爲我產生同樣的結果。我添加了一個'c'屬性,它具有'property:false':輸出將只有'a'和'b',因爲只有那些'property:true'。這不是你想要的輸出嗎?你可以編輯你的問題,並指定期望的輸出是什麼? – trincot

+0

謝謝你,這個更新後的代碼片段確實產生了不同的結果,過濾是正確的,但是返回的數組不幸運,這返回了一個沒有鍵的對象數組,而想要的結果是一個鍵數組。 – Hessius

0

文章的作者提供一個answer in the comments,這裏供參考:

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'] 

進一步閱讀,通過@bergi在原崗位的給予(深埋,貼在這裏的更大的可視性):

  1. jQuery pass more parameters into callback
  2. How do I pass an extra parameter to the callback function in Javascript .filter() method?