Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lodash源码分析之map的实现 #16

Open
HeftyKoo opened this issue Mar 3, 2018 · 1 comment
Open

lodash源码分析之map的实现 #16

HeftyKoo opened this issue Mar 3, 2018 · 1 comment
Labels
api 暴露出来的接口 系列文章

Comments

@HeftyKoo
Copy link
Owner

HeftyKoo commented Mar 3, 2018

宗教与哲学的分野,一个是信仰,一个是怀疑。宗教,稍有怀疑,就被视为异端。

——木心《文学回忆录》

本文为读 lodash 源码的第十五篇,后续文章会更新到这个仓库中,欢迎 star:pocket-lodash

gitbook也会同步仓库的更新,gitbook地址:pocket-lodash

源码分析

function map(array, iteratee) {
  let index = -1
  const length = array == null ? 0 : array.length
  const result = new Array(length)

  while (++index < length) {
    result[index] = iteratee(array[index], index, array)
  }
  return result
}

lodashmap 函数实现很简单,了解 map 用法的应该不难理解。

map 用来实现映射,返回新的数组长度应该跟原数组长度一致,因此一开始就先拿到原数组的 length 属性,创建一个跟原数组长度一致的空数组。

接着遍历,将数组中的每项依次取出,作为第一个参数传递给 iteratee 处理函数。iteratee 的第二个参数为当前项的索引,第三个参数为原数组。然后将 iteratee 返回的结果放入新数组 result 中。

遍历完毕后,将新的数组返回,即实现了 map 函数。

License

署名-非商业性使用-禁止演绎 4.0 国际 (CC BY-NC-ND 4.0)

最后,所有文章都会同步发送到微信公众号上,欢迎关注,欢迎提意见:

作者:对角另一面

@KK-AI-LL
Copy link

这里的应该是arrayMap,其他类型的Map实现比较复杂,作者大大可以说明一下 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api 暴露出来的接口 系列文章
Projects
None yet
Development

No branches or pull requests

2 participants