簡體   English   中英

Lodash使用鏈式重裝

[英]Lodash using rearg in chain

我嘗試過使用帶有和不帶有鏈接的rearg ,並且只有在使用鏈接時才會出現錯誤。 我已經閱讀了文檔,並且看起來rearg應該很好地與鏈接rearg ,因為它返回了可鏈接的包裝器方法。 但是,出現以下錯誤:

Error: _.chain(...).keys(...).myFunc is not a function

使用此代碼:

var myList = ['a','b','c','d']
var myJSON = {
  'a':1,
  'b':2
};

var myFunc = _.rearg(_.difference, [1, 0]); //switching order of arguments

var hasAllKeys = _.chain(myJSON)
                  .keys()
                  .myFunc(myList)
                  .value();

當然,此代碼可以正常工作(盡管輸出不是我所需要的):

var wrong = _.chain(myJSON)
             .keys()
             .difference(myList)
             .values();

我知道這個問題很舊,但這是關於Lodash中的鏈接仍未得到回答的罕見SO問題之一,嘗試回答這個問題使我可以更深入地研究文檔和理解該庫...這是一個很好的機會嘗試堆棧溢出的“運行JS代碼”功能...

我終於明白,它不能像您那樣工作。

首先, rearg真的可以rearg嗎? 答案是:是的。 我嘗試使用以下代碼:

var last = _(twoParams).ary(1).rearg([1, 0]).value();

實際上,當我使用兩個參數調用last時,它僅使用最后一個參數調用twoParams

當然, rearg只能與提供功能作為輸出並期望將功能作為輸入的功能鏈接在一起。 僅在功能鏈中處理功能。

現在,您的代碼不起作用,因為如果rearg是可鏈接的,那么它的輸出將不起作用! 它的輸出是一個普通的舊JavaScript函數,並且這些函數不可鏈接。 好消息是,Lodash以其高超的智慧,提供了一種使函數可鏈接的方法。 您必須使用mixin ,默認情況下,它會將提供的函數添加到Lodash並使其可鏈接。 例如,請參閱使用自定義函數在lodash中創建鏈,該鏈介紹了如何執行此操作。 與這些示例不同,我將遵循Lodash文檔的建議,並使用runInContext避免使用應該保留在本地的新函數來污染Lodash。

這是我的實驗。 我冒昧地重命名了一些標識符,因為我討厭myXxx名稱,更喜歡更具描述性的名稱...

 // Generic code to display resuts var results = document.getElementById('pls-results'); function showHTML(html) { results.insertAdjacentHTML('beforeend', html); } function show(text) { showHTML("<p>" + text + "<p>"); } function showObject(obj) { show("<p>" + JSON.stringify(obj) + "<p>"); } // The real code var keyList = [ 'a', 'b', 'c', 'd' ]; var incompleteJson = { "a": 1, "b": 2 }; var fullJson = { "a": 1, "b": 2, "c": true, "d": "yes" }; // A simple way to do what is implied by the variable name showHTML("<h3>Has All Keys</h3>"); show("Incomplete Json"); var diff = _.difference(keyList, _.keys(incompleteJson)); var hasAllKeys = diff.length === 0; show(hasAllKeys); show("Full Json"); diff = _.difference(keyList, _.keys(fullJson)); hasAllKeys = diff.length === 0; show(hasAllKeys); // What you try to do showHTML("<h3>Consume Expected Keys</h3>"); var localLodash = _.runInContext(); localLodash.mixin( { 'reverseDiff': _.rearg(_.difference, [1, 0]) // switching order of arguments } ); show("Incomplete Json"); var expectedKeys = localLodash.chain(incompleteJson) .keys() .reverseDiff(keyList) .value(); showObject(expectedKeys); show("Full Json"); expectedKeys = localLodash.chain(fullJson) .keys() .reverseDiff(keyList) .value(); showObject(expectedKeys); 
 <div id="pls-results"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> 

該答案是PhiLho出色答案的擴展。 希望這將有助於將來的讀者更多地了解問題和解決方案。 在以下代碼片段中,您可以看到與PhiLho的代碼片段以及其他2個測試相同的結果:

  1. 鏈接_.difference而不是_.reverseDiff :當對象具有在keyList中找不到的某些鍵(而不是作為keyList而不具有對象中的鍵_.reverseDiff時,返回結果。
  2. 最簡單的替代方法( _.difference(myList, _.keys(myJSON)) ):它以較少的代碼返回預期的輸出(此問題的目的可能與該解決方案無關,但是通過顯示此替代方法,您可能可以理解問題更容易)。

 // Generic code to display resuts var results = document.getElementById('pls-results'); function showHTML(html) { results.insertAdjacentHTML('beforeend', html); } // Set up test data var keyList = [ 'a', 'b', 'c', 'd' ]; var incompleteJson = { "a": 1, "b": 2 }; var fullJson = { "a": 1, "b": 2, "c": true, "d": "yes" }; var extraPropertyJson = { "a": 1, "b": 2, "c": true, "d": "yes", 'z': 26 }; // Set up test helper methods var renderLine = function(label, obj) { return "<tr><td>" + label + "</td><td>" + JSON.stringify(obj) + "</td></tr>"; }; var test = function(funcName, funcToTest) { var html = "<h3>" + funcName + "</h3><table>"; html += renderLine("Incomplete Json", funcToTest(incompleteJson)); html += renderLine("Full Json", funcToTest(fullJson)); html += renderLine("Extra Property Json", funcToTest(extraPropertyJson)); html += "</table>"; showHTML(html); }; // The real code var local_ = _.runInContext(); local_.mixin({ 'reverseDiff': _.rearg(_.difference, [1, 0]) // switching order of arguments }); // Tests execution test("_.difference", function(json) { return _.chain(json).keys(). difference(keyList).value(); }); test("local_.reverseDiff", function(json) { return local_.chain(json).keys().reverseDiff(keyList).value(); }); test("_.difference(keyList, _.keys(json))", function(json) { return _.difference(keyList, _.keys(json)); }); 
 <div id="pls-results"></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script> 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM