I've created a function called multiMap, which takes two arrays as parameters and maps them to key value pairs in an object. In some circumstances, the second array of values might be functions, which will become methods in my newly created object.
I want the method values to be invoked with their corresponding key passed in as a string argument. I'm trying this with the line obj[item1].call(item1); but I can't seem to invoke the function correctly this way. Appreciate any help.
let multiMap = (arr1, arr2) => {
const obj = {};
for(let item1 of arr1) {
for(let i = 0; i < arr2.length; i++) {
let item2 = arr2[i]
obj[item1] = item2;
obj[item1].call(item1);
}
}
return obj;
}
function uppercaser(str) { return str.toUpperCase(); }
function capitalize(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }
function repeater(str) { return str + str; }
var items = ['catfood', 'glue', 'beer'];
var functions = [uppercaser, capitalize, repeater];
console.log(multiMap(items, functions)); // should log: { catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }