您的位置 首页 javascript教程

js如何实现原生map

js原生方法map实现,代码如下:

<!DOCTYPE html><html><head>  <meta charset="UTF-8">  <meta name="viewport" content="width=device-width, initial-scale=1.0">  <meta http-equiv="X-UA-Compatible" content="ie=edge">  <meta name="author" content="杨欣">  <title>map</title></head><body>  <script>    Array.prototype.my_map = function (callback) {      if (!Array.isArray(this) || !this.length || typeof callback !== 'function') {        return []      } else {        let result = [];        for (let index = 0; index < this.length; index++) {          const element = this[index];          result.push(callback(element, index, this))        }        return result      }    }    let arr = [1, 2, 3, 4, 5]    let res = arr.my_map((ele, i) => {      return ele + 10    })    console.log(res)  </script></body></html>

文章来源:火云技术网

文章标题:js如何实现原生map

文章地址:https://www.pyhw.net/30466_js%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e5%8e%9f%e7%94%9fmap.html

关于作者: 火云技术网

热门文章

发表评论

您的电子邮箱地址不会被公开。

网站地图