最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在JavaScript中操作數(shù)組之map()方法的使用

 更新時(shí)間:2015年06月09日 09:54:27   投稿:goldensun  
這篇文章主要介紹了在JavaScript中操作數(shù)組之map()方法的使用,是JS入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下

 JavaScript 數(shù)組map()方法創(chuàng)建一個(gè)新的數(shù)組使用調(diào)用此數(shù)組中的每個(gè)元素上所提供的函數(shù)的結(jié)果。
語法

array.map(callback[, thisObject]);

下面是參數(shù)的詳細(xì)信息:

  •     callback : 從當(dāng)前的元素函數(shù)產(chǎn)生新的數(shù)組的元素。
  •     thisObject : 對(duì)象作為該執(zhí)行回調(diào)時(shí)使用

返回值:

返回創(chuàng)建數(shù)組
兼容性:

這種方法是一個(gè)JavaScript擴(kuò)展到ECMA-262標(biāo)準(zhǔn);因此它可能不存在在標(biāo)準(zhǔn)的其他實(shí)現(xiàn)。為了使它工作,你需要添加下面的腳本代碼在頂部:

if (!Array.prototype.map)
{
 Array.prototype.map = function(fun /*, thisp*/)
 {
  var len = this.length;
  if (typeof fun != "function")
   throw new TypeError();

  var res = new Array(len);
  var thisp = arguments[1];
  for (var i = 0; i < len; i++)
  {
   if (i in this)
    res[i] = fun.call(thisp, this[i], i, this);
  }

  return res;
 };
}

例子:

<html>
<head>
<title>JavaScript Array map Method</title>
</head>
<body>
<script type="text/javascript">
if (!Array.prototype.map)
{
 Array.prototype.map = function(fun /*, thisp*/)
 {
  var len = this.length;
  if (typeof fun != "function")
   throw new TypeError();

  var res = new Array(len);
  var thisp = arguments[1];
  for (var i = 0; i < len; i++)
  {
   if (i in this)
    res[i] = fun.call(thisp, this[i], i, this);
  }

  return res;
 };
}

var numbers = [1, 4, 9];
var roots = numbers.map(Math.sqrt);

document.write("roots is : " + roots ); 

</script>
</body>
</html>

這將產(chǎn)生以下結(jié)果:

roots is : 1,2,3 

相關(guān)文章

最新評(píng)論

怀仁县| 汉源县| 北票市| 铜陵市| 中牟县| 金秀| 积石山| 灌云县| 辽阳县| 大同市| 来凤县| 社旗县| 贺兰县| 大化| 乌拉特前旗| 桐城市| 大渡口区| 晋城| 保康县| 海宁市| 保德县| 叙永县| 堆龙德庆县| 丰台区| 呼伦贝尔市| 农安县| 即墨市| 安达市| 博客| 灵川县| 甘德县| 汨罗市| 呼和浩特市| 左贡县| 应城市| 永吉县| 确山县| 南京市| 吴堡县| 湟中县| 凌海市|