Skip to content

Instantly share code, notes, and snippets.

@SephReed
Created August 22, 2019 17:50
Show Gist options
  • Save SephReed/82eb00e44075576d4f778bfd71817c76 to your computer and use it in GitHub Desktop.
Save SephReed/82eb00e44075576d4f778bfd71817c76 to your computer and use it in GitHub Desktop.
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