【问题标题】:JavaScript: Create function that returns an object whose keys match the elements in the array of values using callbacksJavaScript:使用回调创建返回一个对象的函数,该对象的键与值数组中的元素匹配
【发布时间】:2019-10-25 06:46:37
【问题描述】:

我的目标如下:

构造一个函数multiMap,它将接受两个数组——一个值数组和一个回调数组。 multiMap 将返回一个对象,其键与值数组中的元素匹配。分配给键的相应值将是由回调数组的输出组成的数组,其中每个回调的输入是键。

我试过下面的代码:

const multiMap = (arrOne, arrTwo) => {

  let newObj = {}; 

  for (let i=0; i<arrOne.length; i++){
    let element = arrOne[i]; 

    console.log(i)

    newObj[element] = arrTwo[i](element); 
  }
  return newObj; 
}



// Uncomment these to check your work!
function uppercaser(str) { return str.toUpperCase(); }
function capitalize(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }
function repeater(str) { return str + str; }

// arrOne
const items = ['catfood', 'glue', 'beer'];
// arrTwo
const functions = [uppercaser, capitalize, repeater];


console.log(multiMap(items, functions));

我的代码返回:{ catfood: 'CATFOOD', glue: 'Glue', beer: 'beerbeer' }

我希望它返回:{ catfood: ['CATFOOD', 'Catfood', 'catfoodcatfood'], glue: ['GLUE', 'Glue', 'glueglue'], beer: ['BEER', 'Beer', 'beerbeer'] }, 'Beer', 'beerbeer'] }

我做错了什么?

注意:我知道我可以使用修改函数(即 reduce)来做到这一点,但我想先使用 for 循环来解决这个问题。

【问题讨论】:

    标签: javascript function loops callback arguments


    【解决方案1】:

    您必须遍历函数数组才能创建关联的结果数组。

    const multiMap = (arrOne, arrTwo) => {
    
      let newObj = {}; 
    
      for (let i=0; i<arrOne.length; i++){
        let element = arrOne[i]; 
    
        console.log(i)
    
        var arr = [];
        for (let f=0;f<functions.length;f++) {
          arr.push(functions[f](element));
        }
    
        newObj[element] = arr; 
      }
      return newObj; 
    }
    
    
    
    // Uncomment these to check your work!
    function uppercaser(str) { return str.toUpperCase(); }
    function capitalize(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }
    function repeater(str) { return str + str; }
    
    // arrOne
    const items = ['catfood', 'glue', 'beer'];
    // arrTwo
    const functions = [uppercaser, capitalize, repeater];
    
    
    console.log(multiMap(items, functions));

    【讨论】:

      【解决方案2】:

      问题是你只在第一个数组上循环。您必须遍历数组 one 中每个值的所有函数。

      const multiMap = (arrOne, arrTwo) => {
      
        let newObj = {}; 
      
        for (let i=0; i<arrOne.length; i++){
          let element = arrOne[i];
          newObj[element] = [];
          for(let j=0;j < arrTwo.length; j++) {
           
          newObj[element].push(arrTwo[j](element)); 
          }
        }
        return newObj; 
      }
      
      
      
      // Uncomment these to check your work!
      function uppercaser(str) { return str.toUpperCase(); }
      function capitalize(str) { return str[0].toUpperCase() + str.slice(1).toLowerCase(); }
      function repeater(str) { return str + str; }
      
      // arrOne
      const items = ['catfood', 'glue', 'beer'];
      // arrTwo
      const functions = [uppercaser, capitalize, repeater];
      
      
      console.log(multiMap(items, functions));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-01-13
        • 1970-01-01
        • 1970-01-01
        • 2023-01-19
        • 1970-01-01
        • 1970-01-01
        • 2018-10-17
        相关资源
        最近更新 更多