Skip to content

Instantly share code, notes, and snippets.

@morisasy
Created July 12, 2017 09:02
Show Gist options
  • Save morisasy/1868bca09e4c433922f5c866d28490bc to your computer and use it in GitHub Desktop.
Save morisasy/1868bca09e4c433922f5c866d28490bc to your computer and use it in GitHub Desktop.
get elements that equal to 10th at property
Write a function called "getElementsThatEqual10AtProperty".
Given an object and a key, "getElementsThatEqual10AtProperty" returns an array containing all the elements of the array located at the given key that are equal to ten.
Notes:
* If the array is empty, it should return an empty array.
* If the array contains no elements equal to 10, it should return an empty array.
* If the property at the given key is not an array, it should return an empty array.
* If there is no property at the key, it should return an empty array.
function getElementsThatEqual10AtProperty(obj, key) {
// your code here
var array = [];
//check if obj[key] is true
if(obj[key]){
// looping through an obj.
for(var i in obj[key]){
// testing if obj value is 10
if(obj[key][i]== 10){
array.push(obj[key][i]);
}
}
return array;
}
return array;
}
@Josef2021
Copy link

Thanks this was helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment