Last active
March 22, 2020 19:58
-
-
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
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
// 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