小程序 array.map

Array.map might be JavaScript's most useful function. Forgoing it nowadays is like donning your toolbelt without a hammer.

Array.map可能是JavaScript最有用的功能。 如今,放弃它就像没有锤子就穿上工具带。

To further appreciate map and deepen our understanding of it, let's see 4 levels of explanations in ascending complexity.

为了进一步了解map并加深对map的理解,让我们看一下关于复杂性递增的4个层次的解释。

Tell me which ones you got and which ones surprised you!

告诉我您得到了哪些,哪些使您感到惊讶!

目录 (Table of Contents)

  1. To a Five Year Old

    到五岁

  2. To a High School Coding Student

    致高中编码学生

  3. To a React Developer

    致React开发人员

  4. To a Functional Programmer

    致功能程序员

到五岁 (To a Five Year Old)

Do you know DragonBall Z? Here are my favorite characters from the show!

你知道七龙珠Z吗? 这是节目中我最喜欢的角色!

悟空 (Goku)

素食 (Vegeta)

皮箱 (Trunks)

They're saiyans, and they're really strong!

他们是赛亚人,他们真的很强!

I put them in this list–JavaScript calls them arrays. It lets you hold a bunch of things together:

我将它们放在此列表中-JavaScript称它们为arrays 。 它使您可以将一堆东西放在一起:

saiyans = [goku, vegeta, trunks];

And I have code that turns them into Super Saiyans, so they get 50x stronger (literally)! This code is called a function.

而且我有将他们变成超级赛亚人的代码,因此它们的强度提高了50倍! 此代码称为函数

turnSuperSaiyan = () => { /* use your imagination */ };
turnSuperSaiyan(goku);

What if I want to transform all 3 of them? I have to run the function 3 times! Repeating things like that is boring ?

如果我要转换所有三个呢? 我必须运行该功能3次! 重复这样的事情很无聊吗?

turnSuperSaiyan(goku);
turnSuperSaiyan(vegeta);
turnSuperSaiyan(trunks);

Luckily, programming lets you repeat things lots of times really easily! Array.map can turn them all into Super Saiyans at once!

幸运的是,编程使您可以真正轻松地重复很多次! Array.map可以一次将它们全部变成超级赛亚人!

Just plug turnSuperSaiyan in there and get back a new array of Super Saiyan Goku, Vegeta, and Trunks.

只需将turnSuperSaiyan插入那里,然后获得的Super Saiyan Goku,Vegeta和Trunks 阵列

superSaiyans = saiyans.map(turnSuperSaiyan);

致高中编码学生 (To a High School Coding Student)

Hi!

嗨!

So you've learned for loops. They're great for performing repetitive work, but I personally haven't needed one in years.

因此,您已经了解for循环。 它们非常适合执行重复性工作,但我个人多年以来都不需要。

Don't get me wrong, I still love automating repetitive work. In fact, most applications involve repetitive work.

不要误会我的意思,我仍然喜欢自动化重复性工作。 实际上,大多数应用程序涉及重复性工作。

Think of these examples...

想想这些例子...

  • InstagramInstagram
  • WhatsappWhatsapp的
  • Google search resultsGoogle搜寻结果
  • Emails电邮
  • Contacts联络人
  • Text messages短信

If you boil them down to the core, these everyday apps are just fancy lists. Much of Front-End development is transforming these lists into something user-friendly.

如果您将它们归结为核心,那么这些日常应用程序只是花哨的清单。 许多前端开发正在将这些列表转换为用户友好的东西。

Of course the big picture is more complex, but the core of most apps is manipulating lists!

当然,大局要复杂得多,但是大多数应用程序的核心是操纵列表!

In a JavaScript program, we represent lists as arrays.

在JavaScript程序中,我们将列表表示为数组。

All arrays carry a special method called map. It lets you transform an array into a new one based on some function you give it.

所有数组都带有一种称为map的特殊方法。 它使您可以根据提供的某些功能将数组转换为新数组。

Here's some numbers.

这是一些数字。

numbers = [1, 2, 3, 4, 5];

And a double function.

并具有double功能。

double = (x) => x * 2;

Can you double each one using a for loop?

您可以使用for循环将每个人加倍吗?

doubledNumbers = [];for (let i = 0; i < numbers.length; i++) {doubledNumbers.push(double(numbers[i]))
}// [2, 4, 6, 8, 10]

Cool! Here's the same idea expressed with map.

凉! 这是map表达的相同想法。

doubledNumbers = numbers.map(double);
// [2, 4, 6, 8, 10]

map constructs the loop under the hood, so you don't have to worry about typos or missing semicolons anymore!

map在后台构造循环,因此您不必担心拼写错误或缺少分号!

And this goes beyond just numbers. Here's some users...

这不仅限于数字。 这是一些用户...

users = [{name: 'Bruce Wayne',location: 'Gotham City',heroName: 'Batman'
}, {name: 'Barry Allen',location: 'Central City',heroName: 'The Flash'
}, {name: 'Clark Kent',location: 'Kryptonopolis',heroName: 'Superman'
}];

How would you create a new array of every user's name and heroName? Probably using a for loop.

您将如何创建一个包含每个用户nameheroName 的新数组 ? 可能使用for循环。

userInfo = [];for (let i = 0; i < users.length; i++) {userInfo.push({name: users[i].name,heroName: users[i].heroName});
}// Result
[{"name": "Bruce Wayne","heroName": "Batman"},{"name": "Barry Allen","heroName": "The Flash"},{"name": "Clark Kent","heroName": "Superman"}
]

Here's a loop-less version.

这是无循环版本。

userInfo = users.map(u => ({name: u.name,heroName: u.heroName
}));// Result
[{"name": "Bruce Wayne","heroName": "Batman"},{"name": "Barry Allen","heroName": "The Flash"},{"name": "Clark Kent","heroName": "Superman"}
]

See how much easier that is? We can implement map like so:

看看这有多容易? 我们可以像这样实现map

map = (fn, array) => {const results = [];for (let i = 0; i < array.length; i++) {results.push(fn(array[i]));}return results;
}

So for every element, call the given function and store it inside a new array!

因此,对于每个元素,调用给定的函数并将其存储在新数组中!

致React开发人员 (To a React Developer)

Hi!

嗨!

The Array prototype offers a method called map.

Array原型提供了一种称为map的方法。

It will loop over your array, calling a given function on each item, and return a new array with those changes.

它将遍历您的数组,在每个项目上调用给定的函数,并返回具有这些更改的新数组。

Instead of a for loop, just use map to get usernames and render the UI.

代替for循环,只需使用map获取用户名并呈现UI。

const App = users => {return (<ul><li>My name is {users.map(u => u.name)}!</li></ul>);
};

Yep you can method chain, since it returns the same type!

是的,您可以使用方法链,因为它返回相同的类型!

const App = users => {return (<ul>{users.map(u => u.name).map(name => (<li>My name is {name}!</li>))}</ul>);
};

Tremendously useful. Most of your main components will probably use map.

非常有用。 您的大多数主要组件都可能会使用map

致功能程序员 (To a Functional Programmer)

Map simply lifts a function a -> b into a context F a -> F b.

Map只是将函数a- a -> b提升到上下文F a -> F b

JavaScript doesn't extend this expressibility beyond arrays, unfortunately...

不幸的是,JavaScript并没有将这种可扩展性扩展到数组之外。

Thank you Brian Lonsdorf for the wicked explanation!

谢谢Brian Lonsdorf 的邪恶解释 !

For more content like this, check out https://yazeedb.com!

有关更多内容,请访问https://yazeedb.com!

And please let me know what else you'd like to see! My DMs are open for questions, comments, and suggestions!

并且,请让我知道您还想看到什么! 我的DM对问题,评论和建议开放!

翻译自: https://www.freecodecamp.org/news/array-map-explained-in-4-levels-of-difficulty/

小程序 array.map

小程序 array.map_Array.map解释了4个复杂级别:从5岁到功能程序员。相关推荐

  1. 微信小程序API之map

    微信小程序API之map wxml: <map id="map" longitude="113.324520" latitude="23.099 ...

  2. 小撸 array map 源码

    官方的方法有时候用着确实挺好用的,那仔细想想是怎样实现的呢?自己是否可以搞一个,那就从最简单的开始吧 Array.prototype.map 要撸一个array map方法的源码,首先需要看下他的具体 ...

  3. hive中array嵌套map以及行转列的使用

    1. 数据源信息 {"student": {"name":"king","age":11,"sex" ...

  4. array.prototype.map()如何工作

    by Pradeep Pothineni 通过Pradeep Pothineni array.prototype.map()如何工作 (How array.prototype.map() works) ...

  5. JS中集合对象(Array、Map、Set)及类数组对象的使用与对比

    JS中集合对象(Array.Map.Set)及类数组对象的使用与对比 在使用js编程的时候,常常会用到集合对象,集合对象其实是一种泛型,在js中没有明确的规定其内元素的类型,但在强类型语言譬如Java ...

  6. 计算机天天乐学题库C语言,天天乐学选择题(共1919小题)-有答案及解释.docx

    天天乐学选择题(共1919小题)-有答案及解释.docx 天天乐学选择题共1919小题1.利用WINDOWS7"搜索"功能查找文件时,说法正确的是.A.要求被查找的文件必须是文本文 ...

  7. Go程序:演示map用法

    Go程序:演示map用法 在C++/Java中,map一般都以库的方式提供,比如在C++中是STL的std::map<>,在C#中是 Dictionary<>,在Java中是H ...

  8. 小程序源码:游戏助手王者战力查询扫码登录多功能微信小程序

    这是一款游戏多功能助手小程序 内由王者战力查询(支持微信QQ双端查询,安卓IOS) 游戏扫码登录(内支持多种游戏扫码登录) 短视频去水印功能(支持各大平台) 游戏改名助手(支持空白名生成,符号名生成) ...

  9. hive 复杂数据类型 在数仓中应用(array、map、struct、和其组合应用)

    环境:一般宽表建表可能考虑存储更多信息选择复杂模型建设 复杂数据类型:array.map.struct 1.数组array,里边不能装不同类型的数据 more hive_array.txt zhang ...

最新文章

  1. Python List append()方法
  2. 快乐的生活 2008-10-10 18:49:00
  3. mysqld:表mysql.plugin不存在_99%测试工程师不知道的数据库知识|干货
  4. 阿帕奇退出java_java+tomcat+apache安装整合,启动/关闭,添加开机启动服务
  5. 这是一次 docker 入门实践
  6. 符号实体(转义字符)
  7. 微信开发 调用摄像机拍照(录像)功能
  8. [Swift]LeetCode198. 打家劫舍 | House Robber
  9. 百分字符知识付费教程
  10. (附源码)计算机毕业设计ssm电影票网上订票系统
  11. TI-TMS320F28335学习详解(2)::F28335片上资源详解
  12. Python电商数据分析实战案例
  13. oeasy教您玩转vim - 88 - # 自动命令autocmd
  14. Java基础(二)标识符、变量、数据类型、运算符
  15. kindle 越狱降级
  16. 联想笔记本电脑insert键占用,当鼠标变成小方块时,如何解决
  17. PCIe数据卡设计资料第611篇:基于VU9P的双路5Gsps AD 双路6Gsps DA PCIe数据卡
  18. 记录下公司刻录新版本/repo_kitkat 命令
  19. 移动物联网项目搭建(一)——起步
  20. 向Facebook学什么

热门文章

  1. 【今日CS 视觉论文速览】Wed, 12 Dec 2018
  2. Java—线程的生命周期及线程控制方法详解
  3. 主窗体的常用属性 c# 1615011872
  4. python-函数的使用 0222
  5. 基于Node.js实现压缩和解压缩的方法
  6. Oracle GoldenGate For Big Data - Kafka Handler
  7. ASP.NET Web API 接口执行时间监控
  8. polymer中的sort和filter
  9. 学习了时间和测试题目
  10. 炫酷实用 7款jQuery/HTML5图片应用