Skip to content

Instantly share code, notes, and snippets.

@morisasy
Last active July 12, 2017 14:14
Show Gist options
  • Save morisasy/3c5f3b081b052970be232f9b11956701 to your computer and use it in GitHub Desktop.
Save morisasy/3c5f3b081b052970be232f9b11956701 to your computer and use it in GitHub Desktop.
Write a function called "getFirstElementOfProperty". Given an object and a key, "getFirstElementOfProperty" returns the first element of the array located at the given key.
Notes:
* If the array is empty, it should return undefined.
* If the property at the given key is not an array, it should return undefined.
* If there is no property at the key, it should return undefined.
function getFirstElementOfProperty(obj, key) {
// your code here
if ( Array.isArray( obj[key] ) === false ){
return undefined;
}else if ( obj[key][0] === undefined){
return undefined;
}else{
return obj[key][0];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment