簡體   English   中英

使用模糊匹配在集合中查找對象

[英]Find object in collection with fuzzy matching

我有一個看起來像這樣的集合:

const collection = [
    {
        name: 'THIS_ITEM',
        conditions: {
            oneCondition: false,
            anotherCondition: false,
            yourCondition: false,
            myCondition: false
        }
    }, {
        name: 'THAT_ITEM',
        conditions: {
            oneCondition: false,
            anotherCondition: false,
            yourCondition: true,
            myCondition: false
        }
    }, {
        name: 'THOSE_ITEMS',
        conditions: {
            oneCondition: true,
            anotherCondition: false,
            yourCondition: null,
            myCondition: false
        }
    }
];

......以及后來看起來像這樣的對象:

const condition = {
    oneCondition: true,
    anotherCondition: false,
    yourCondition: true,
    myCondition: false
};

我正在嘗試將condition對象與collection中的嵌套conditions對象進行匹配,以找到匹配的對象,這樣我就可以從匹配的條目中檢索name屬性。

拋棄循環的事情是conditions屬性可以具有“模糊”值。 我的意思是,如果源collection中的任何屬性設置為truefalse它們必須完全匹配condition中的值。 但是,如果源在屬性collection的值為null它可以匹配 truefalse

例:

這些將匹配:

const condition = {
    oneCondition: true,
    anotherCondition: false,
    yourCondition: true,
    myCondition: false
};

const collection = [
    …
    }, {
        name: 'THOSE_ITEMS',
        conditions: {
            oneCondition: true,
            anotherCondition: false,
            yourCondition: null,
            myCondition: false
        }
    }
];

這些不會:

const condition = {
    oneCondition: true,
    anotherCondition: false,
    yourCondition: true,
    myCondition: false
};

const collection = [
    …
    }, {
        name: 'THAT_ITEM',
        conditions: {
            oneCondition: false,
            anotherCondition: false,
            yourCondition: true,
            myCondition: false
        }
    }, {
    …
];

有什么建議? 我正在使用Lodash,但似乎無法想象沒有過於冗長和嵌套的混合物的任何解決方案。

您可以將Array#filterArray#every用於條件,並將null值作為通配符進行測試。

 var collection = [{ name: 'THIS_ITEM', conditions: { oneCondition: false, anotherCondition: false, yourCondition: false, myCondition: false } }, { name: 'THAT_ITEM', conditions: { oneCondition: false, anotherCondition: false, yourCondition: true, myCondition: false } }, { name: 'THOSE_ITEMS', conditions: { oneCondition: true, anotherCondition: false, yourCondition: null, myCondition: false } }], condition = { oneCondition: true, anotherCondition: false, yourCondition: true, myCondition: false }, result = collection.filter(o => Object.keys(condition).every(k => o.conditions[k] === null || o.conditions[k] === condition[k] ) ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

您可以將lodash#filterlodash#isMatchlodash#omitBy以將condition與不包含任何null值的集合對象的條件進行匹配。

const result = _.filter(collection, v => 
  _.isMatch(condition, _.omitBy(v.conditions, _.isNull))
);

 const collection = [ { name: 'THIS_ITEM', conditions: { oneCondition: false, anotherCondition: false, yourCondition: false, myCondition: false } }, { name: 'THAT_ITEM', conditions: { oneCondition: false, anotherCondition: false, yourCondition: true, myCondition: false } }, { name: 'THOSE_ITEMS', conditions: { oneCondition: true, anotherCondition: false, yourCondition: null, myCondition: false } } ]; const condition = { oneCondition: true, anotherCondition: false, yourCondition: true, myCondition: false }; const result = _.filter(collection, v => _.isMatch(condition, _.omitBy(v.conditions, _.isNull)) ); console.log(result); 
 body > div { min-height: 100%; top: 0; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script> 

您可以使用lodash的_.isMatchWith()過濾收集:

 const condition = {"oneCondition":true,"anotherCondition":false,"yourCondition":true,"myCondition":false}; const collection = [{"name":"1","conditions":{"oneCondition":true,"anotherCondition":false,"yourCondition":true,"myCondition":false}},{"name":"2","conditions":{"oneCondition":true,"anotherCondition":false,"yourCondition":false,"myCondition":false}},{"name":"3","conditions":{"oneCondition":true,"anotherCondition":false,"yourCondition":null,"myCondition":false}}]; const result = collection.filter(({ conditions }) => _.isMatchWith(condition, conditions, (objValue, othValue) => objValue === null || othValue === null || objValue === othValue) // if at least one of the values is null or they are equal ); console.log(result); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> 

你可以使用lodash .filter()的組合 .every().isEqual()方法循環遍歷collection並僅過濾與對象具有相同條件的項:

_.filter(collection, function(c) {
  return _.every(_.keys(condition), function(currentKey) {
    return c.conditions[currentKey] === null ||
      _.isEqual(c.conditions[currentKey], condition[currentKey]);
  });
});

演示:

 const collection = [{ name: 'THIS_ITEM', conditions: { oneCondition: false, anotherCondition: false, yourCondition: false, myCondition: false } }, { name: 'THAT_ITEM', conditions: { oneCondition: false, anotherCondition: false, yourCondition: true, myCondition: false } }, { name: 'THOSE_ITEMS', conditions: { oneCondition: true, anotherCondition: false, yourCondition: null, myCondition: false } }]; const condition = { oneCondition: true, anotherCondition: false, yourCondition: true, myCondition: false }; var result = _.filter(collection, function(c) { return _.every(_.keys(condition), function(currentKey) { return c.conditions[currentKey] === null || _.isEqual(c.conditions[currentKey], condition[currentKey]); }); }); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 
 <script src="http://underscorejs.org/underscore-min.js"></script> 

暫無
暫無

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

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