JavaScript数组地图方法回调参数 [英] Javascript array map method callback parameters

查看:145
本文介绍了JavaScript数组地图方法回调参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

描述的Javascript地图方法的语法如下:

arr.map(回调[,thisArg])

参数


  • 回调 - 功能产生新的数组的元素,取三个参数:

  • CurrentValue的阵列中被处理-T​​he当前元素。

  • 指数 - 当前元素的索引数组中被处理

  • 阵列阵图是呼吁。

  • thisArg - 价值,因为这时候执行回调使用

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

我可以按如下方式写一个JS函数,大写每个单词的第一个字母的字符串数组:

  capyyIt(['约翰','纳尔逊·曼德拉,詹姆斯展台'])//返回['约翰','纳尔逊·曼德拉,詹姆斯展台']功能capyyIt(地名){
  返回names.map(功能capIt(CURR){返回CURR [0] .toUpperCase()+ curr.slice(1).toLowerCase();});
}


  1. 如何写capIt有4个参数(即使我可能没有使用它们全部)?我这么问是因为我有4个参数尝试过,但它似乎没有工作。

  2. 我怎么能写capIt作为非立即调用函数前pression - 是这样的:

     函数capyyIt(地名){
        返回names.map(capIt);
    }
    功能capIt(CURR,指数,编曲){
        返回CURR [0] .toUpperCase()+ curr.slice(1).toLowerCase();
    }



解决方案

我不是你问完全清楚,但让我们看看,如果这是你在找什么:

 函数capyyIt(地名){
  names.map(功能(价值指数,集合){
     capIt(VAL);
  })
}功能capIt(名称){
  返回name.toUpperCase()+ curr.slice(1).toLowerCase();
}

地图功能需要使用该值,索引和集合作为参数的回调参数,你无法改变这一点。因此,如果你想要做的事,以每个值被映射,你应该这样做回调函数内。

Javascript describes the map method syntax as:

arr.map(callback[, thisArg])

Parameters

  • callback - Function that produces an element of the new Array, taking three arguments:
  • currentValue -The current element being processed in the array.
  • index - The index of the current element being processed in the array.
  • array The array map was called upon.
  • thisArg - Value to use as this when executing callback.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

I can write a JS function that capitalizes the first letter of each word in an array of strings as follows:

capyyIt(['john', 'nelson mandela', 'james booth'])     // returns ['John', 'Nelson mandela', 'James booth']

function capyyIt(names) {
  return names.map(function capIt(curr) { return curr[0].toUpperCase() + curr.slice(1).toLowerCase(); });
}

  1. How can I write capIt with 4 parameters (even though I may not be using them all)? I'm asking because I tried it with 4 parameters but it didn't seem to work.
  2. How can I write capIt as a non-immediately invoked function expression - something like:

    function capyyIt(names) {
        return names.map(capIt);
    }
    function capIt(curr,index,arr) {
        return curr[0].toUpperCase() + curr.slice(1).toLowerCase();
    }
    

解决方案

I'm not entirely clear on what you're asking but lets see if this is what you're looking for:

function capyyIt(names) {
  names.map(function(value, index, collection){
     capIt(val);
  })
}   

function capIt(name) {
  return name.toUpperCase() + curr.slice(1).toLowerCase();
}

The map function takes a callback argument that uses the value, index, and collection as arguments, you can't change this. Therefore if you want to do something to each value being mapped, you should do it inside the callback function.

这篇关于JavaScript数组地图方法回调参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆