Created
December 11, 2017 20:31
-
-
Save amandarfernandes/aea97472e07e665d6cab5064a9354b8c to your computer and use it in GitHub Desktop.
JSArrayFilter
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
/* | |
Write a function called filterByValue which accepts an array of objects and a key and returns a new array with all the objects that contain that key. | |
Examples: | |
filterByValue([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}], 'isCatOwner') // [{first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Colt', last:"Steele", isCatOwner: true}] | |
*/ | |
function filterByValue(arr, key){ | |
return arr.filter(function(value){ | |
return key in value; | |
}); | |
} | |
/* | |
Write a function called find which accepts an array and a value and returns the first element in the array that has the same value as the second parameter or undefined if the value is not found in the array. | |
Examples: | |
find([1,2,3,4,5], 3) // 3 | |
find([1,2,3,4,5], 10) // undefined | |
*/ | |
function find(arr, searchValue){ | |
var fArr = arr.filter(function(value){ | |
return value === searchValue; | |
}); | |
if (fArr.length>0) return fArr[0]; | |
} | |
/* | |
Write a function called findInObj which accepts an array of objects, a key, and some value to search for and returns the first found value in the arrayt. | |
Examples: | |
findInObj([{first: 'Elie', last:"Schoppik"}, {first: 'Tim', last:"Garcia", isCatOwner: true}, {first: 'Matt', last:"Lane"}, {first: 'Colt', last:"Steele", isCatOwner: true}], 'isCatOwner',true) // {first: 'Tim', last:"Garcia", isCatOwner: true} | |
*/ | |
function findInObj(arr, key, searchValue){ | |
var newArr =arr.filter(function(value){ | |
return key in value && value[key] === searchValue; | |
}); | |
if (newArr.length > 0) return newArr[0]; | |
} | |
/* | |
Write a function called removeVowels which accepts a string and returns a new string with all of the vowels (both uppercased and lowercased) removed. Every character in the new string should be lowercased. | |
Examples: | |
removeVowels('Elie') // ('l') | |
removeVowels('TIM') // ('tm') | |
removeVowels('ZZZZZZ') // ('zzzzzz') | |
*/ | |
function removeVowels(str){ | |
var arr = str.split(''); | |
var vowels = ['a','e','i','u']; | |
return arr.filter(function(value){ | |
return vowels.indexOf(value.toLowerCase()) === -1; | |
}).join('').toLowerCase(); | |
} | |
/* | |
Write a function called doubleOddNumbers which accepts an array and returns a new array with all of the odd numbers doubled (HINT - you can use map and fitler to double and then filter the odd numbers). | |
Examples: | |
doubleOddNumbers([1,2,3,4,5]) // [2,6,10] | |
doubleOddNumbers([4,4,4,4,4]) // [] | |
*/ | |
function doubleOddNumbers(arr){ | |
return arr.filter(function(value){ | |
return value%2 !==0; | |
}).map(function(val){ | |
return val*2; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment