Skip to content

Instantly share code, notes, and snippets.

@amandarfernandes
Created December 11, 2017 20:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amandarfernandes/e0506c08631611a33f002a11fca5e52b to your computer and use it in GitHub Desktop.
Save amandarfernandes/e0506c08631611a33f002a11fca5e52b to your computer and use it in GitHub Desktop.
JSArrayReduce
/*
Write a function called extractValue which accepts an array of objects and a key and returns a new array with the value of each object at the key.
Examples:
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}]
extractValue(arr,'name') // ['Elie', 'Tim', 'Matt', 'Colt']
*/
function extractValue(arr, key){
return arr.reduce(function(accum,value,i) {
return accum = accum.concat(value[key]);
},[]);
}
/*
Write a function called vowelCount which accepts a string and returns an object with the keys as the vowel and the values as the number of times the vowel appears in the string. This function should be case insensitive so a lowercase letter and uppercase letter should count
Examples:
vowelCount('Elie') // {e:2,i:1};
vowelCount('Tim') // {i:1};
vowelCount('Matt') // {a:1})
vowelCount('hmmm') // {};
vowelCount('I Am awesome and so are you') // {i: 1, a: 4, e: 3, o: 3, u: 1};
*/
function vowelCount(str){
return str.split("").reduce(function(accum,value) {
var val = value.toLowerCase();
if (["a","e","i","o","u"].indexOf(val) >-1) {
if (val in accum) {
accum[val]++;
} else {
accum[val]=1;
}
}
return accum;
},{});
}
/*
Write a function called addKeyAndValue which accepts an array of objects and returns the array of objects passed to it with each object now including the key and value passed to the function.
Examples:
var arr = [{name: 'Elie'}, {name: 'Tim'}, {name: 'Matt'}, {name: 'Colt'}];
addKeyAndValue(arr, 'title', 'Instructor') //
[
{title: 'Instructor', name: 'Elie'},
{title: 'Instructor', name: 'Tim'},
{title: 'Instructor', name: 'Matt'},
{title: 'Instructor', name: 'Colt'}
]
*/
function addKeyAndValue(arr, key, value){
return arr.reduce(function(accum,val) {
val[key] = value;
accum = accum.concat(val);
return accum;
},[]);
}
/*
Write a function called partition which accepts an array and a callback and returns an array with two arrays inside of it. The partition function should run the callback function on each value in the array and if the result of the callback function at that specific value is true, the value should be placed in the first subarray. If the result of the callback function at that specific value is false, the value should be placed in the second subarray.
Examples:
function isEven(val){
return val % 2 === 0;
}
var arr = [1,2,3,4,5,6,7,8];
partition(arr, isEven) // [[2,4,6,8], [1,3,5,7]];
function isLongerThanThreeCharacters(val){
return val.length > 3;
}
var names = ['Elie', 'Colt', 'Tim', 'Matt'];
partition(names, isLongerThanThreeCharacters) // [['Elie', 'Colt', 'Matt'], ['Tim']]
*/
function partition(arr, callback){
return arr.reduce(
function(accum,value) {
if (callback(value))
accum[0] = accum[0].concat(value);
else
accum[1] = accum[1].concat(value);
return accum;
},[[],[]]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment