Find an object in the collection with a fuzzy match

I have a collection that looks something like this:

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

... and then an object that looks like this:

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

Im trying to match an object conditionwith nested conditionsobjects in collectionto find the one that matches so that I can get the property namefrom the corresponding entry.

The thing that throws me into the loop is that properties conditionscan have “fuzzy” values. By this I mean that if any properties in the source are collectionset to trueor false, they MUST exactly match the values ​​in condition. But if the property in the source collectionmatters null, it can match either true, or false.

Example:

:

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, - .

+4
4

Array#filter Array#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; }
+2

lodash#filter lodash#isMatch lodash#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>
+1

, 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>
0

You can use a combination of lodash  .filter () , .every () and .isEqual () to iteratecollection and filter only those objects that have the same conditions as your object:

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

Demo:

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>
Run code
0
source

Source: https://habr.com/ru/post/1676815/


All Articles