Created
August 22, 2019 17:50
-
-
Save SephReed/82eb00e44075576d4f778bfd71817c76 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Here's a vanilly implementation. Just use object keys to check that all of the props have matching values. | |
<!-- begin snippet: js hide: false console: true babel: false --> | |
<!-- language: lang-js --> | |
function findPropsMatch(list, target) { | |
const props = Object.keys(target); | |
return list.find((checkMe) => | |
!props.find((prop) => checkMe[prop] !== target[prop]) | |
); | |
} | |
const items = [ | |
{a: 1, b: 2, c: "Hey"}, | |
{a: 2, b: 2, c: "Hi"}, | |
] | |
console.log(findPropsMatch(items, {a:1, b:2})); // Hey | |
console.log(findPropsMatch(items, {b:2})); // Hey | |
console.log(findPropsMatch(items, {a:2})); // Hi | |
console.log(findPropsMatch(items, {a:3})); // undefined | |
<!-- end snippet --> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment