Skip to content

Instantly share code, notes, and snippets.

@carlok
Last active March 22, 2020 19:58
Show Gist options
  • Save carlok/fa2bda1e713fd95388e9e876e0f0a63a to your computer and use it in GitHub Desktop.
Save carlok/fa2bda1e713fd95388e9e876e0f0a63a to your computer and use it in GitHub Desktop.
ES6 checking if exists an obj in a obj array with a given key/value
// clean up based on
// https://medium.com/@rafaeloliveira1337/javascript-find-an-object-in-array-based-on-objects-property-8adb4fe421ce
// fiddle => https://jsfiddle.net/carlok/d5j4t67h/
const objArray = [
{ id: 0, name: 'Object 0', otherProp: '321' },
{ id: 1, name: 'foo', otherProp: '648' },
{ id: 2, name: 'Another Object', otherProp: '850' },
{ id: 3, name: 'Almost There', otherProp: '046' },
{ id: 4, name: 'foo2', otherProp: '984' }
]
const obj = objArray.find(obj => obj.name === 'foo')
console.log(obj === undefined ? 'free' : 'taken')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment