python 构件二维数组

Arrays in JavaScript are something special, as they leverage the prototype feature of JS and provide a handy way to run functions directly from a reference to an array instance.

JavaScript中的数组很特别,因为它们利用了JS的原型功能,并提供了一种便捷的方法来直接从引用到数组实例运行函数。

I believe that using these four functions as much as possible within your code will not only make you more efficient, but also make you more popular on your team and generally more amazing at what you do.

我相信,在您的代码中尽可能多地使用这四个功能不仅会提高您的效率,而且还会使您在团队中更受欢迎,并且通常会使您的工作更加惊奇。

All four of these prototype functions accept a “callback” function, which is a pure function that accepts arguments and must return something expected back to the caller, whether it be a boolean liketrue or false, or a type like String, Object, Array, or Number.

所有这四个原型函数都接受一个“回调”函数,这是一个纯函数,它接受参数并且必须将期望的值返回给调用者,无论它是booleantruefalse )还是类型(如StringObjectArrayNumber

Let’s take a look at each of these useful Array functions and learn to make our code more concise!

让我们看一下这些有用的Array函数,并学习使我们的代码更简洁 !

地图— Array.prototype.map() (Map — Array.prototype.map())

Photo by Louis Hansel @shotsoflouis on Unsplash
Louis Hansel的照片@shotsoflouis在Unsplash上

map is a building block of most programming languages, and is definitely a wonderful Array function to start with.

map是大多数编程语言的构建块,并且绝对是一个很棒的Array函数。

This line of code for instance, will loop through ANIMALS and create an array of Strings, with their name and animal type:

例如,以下代码行将遍历ANIMALS并创建Strings数组,其名称和动物类型为:

You might already be familiar with map if you are using front-end frameworks like React or Vue where you quite often need to loop through an array of data to produce HTML:

如果您使用的是React或Vue等前端框架,那么您可能已经很熟悉map ,在这些框架中,您经常需要遍历数据数组以生成HTML:

<ul>    {ANIMALS.map((animal) =>        <li>            {animal.name} ({animal.type})        </li>    )}</ul>​// produces:<ul>    <li>Spot (dog)</li>    <li>Fluffy (cat)</li>    <li>Peppy (bird)</li>    <li>Lizzy (lizard)</li></ul>

The map function is all-around useful to convert array of something X into array of something Y.

map函数可用于将X数组转换为Y数组

Reduce — Array.prototype.reduce() (Reduce — Array.prototype.reduce())

The reduce function is used like map but takes it even further.

reduce函数的用法与map一样,但功能更进一步。

Instead of convert array of something X into array of something Y, reduce is designed to convert array of something X into something Y, meaning you are not limited to just making a new array.

reduce不是将X的数组转换为Y的数组,而是设计了将X的数组转换为Y的意思,这意味着您不仅限于制作新的数组

You can turn an array into Objects, Numbers, or Strings. Here we sum up the total weight of all of the ANIMALS:

您可以将数组转换为ObjectsNumbersStrings 。 在这里,我们总结所有ANIMALS的总重量:

reduce is such a powerful function on arrays, that it is versatile enough to produce the same effect as a map does. Check out this reduce version of a map function:

reduce是数组上如此强大的功能,它具有足够的通用性,可以产生与map相同的效果。 看看这个map函数的reduce版本:

筛选器— Array.prototype.filter() (Filter — Array.prototype.filter())

Photo by Caleb Dow on Unsplash
Caleb Dow在Unsplash上拍摄的照片

filter works just as it sounds, and only retrieves items from an array that produce a truthy value from your callback function. Here we look for ANIMALS that have soft fur or feathers:

filter工作原理与听起来一样,仅从可从回调函数产生真实值的数组中检索项目。 在这里,我们寻找具有柔软皮毛或羽毛的ANIMALS

Here is another example, using a mathematical comparison. As long as the function returns a truthy/falsy value it works as expected. Here we only select ANIMALS that weigh less than 10lbs:

这是另一个使用数学比较的例子。 只要函数返回真实/虚假值,它就会按预期工作。 在这里,我们只选择重量小于10磅的ANIMALS

查找— Array.prototype.find() (Find — Array.prototype.find())

Finally, we have find, which helps you search through an array using a callback. This is extremely useful as it provides an interface to search through an array that also works with a sister function called findIndex.

最后,我们find ,它可以帮助您使用callback遍历数组。 这非常有用,因为它提供了一个搜索数组的接口,该数组也可以与名为findIndex的姊妹函数findIndex

As long as your function returns a truthy value, the first truthy value returned will exit the find search and return back that row from the array.

只要您的函数返回真实值,返回的第一个真实值将退出find搜索并从数组中返回该行。

In this example, we find a dog named Spot:

在此示例中,我们find了一只名为Spot的狗:

组合数组函数 (Combining Array Functions)

Photo by Ali Yahya on Unsplash
Ali Yahya在Unsplash上拍摄的照片

Now that you have a handle on these four building block functions, we can combine them all together:

现在您已经掌握了这四个构建基块函数,我们可以将它们全部组合在一起:

Above, we first map over our ANIMALS and create a new object of their weight and type, also taking this opportunity to feed each one. We feed them and they might gain about 1% of their body weight after eating - this is just a guess.

上面,我们首先在ANIMALS map ,并创建一个重量类型相同的新对象,并借此机会喂食每个ANIMALS 。 我们喂养他们,他们进食后可能会增加体重的1%-这只是一个猜测。

Then we filter out only animals that have fur or feathers, notice we did this after feeding, since we don’t want to starve our lizard Lizzy

python 构件二维数组_通过这四个构件块来升级您的javascript数组相关推荐

  1. python画二维图_使用python绘制二维图形示例

    我就废话不多说了,直接上代码吧! import matplotlib.pyplot as plt #也可以使用 import pylab as pl import matplotlib.font_ma ...

  2. python绘制二维图形_使用python绘制二维图形示例

    我就废话不多说了,直接上代码吧! import matplotlib.pyplot as plt #也可以使用 import pylab as pl import matplotlib.font_ma ...

  3. 用python制作二维码_使用python制作二维码

    python-qrcode是个用来生成二维码图片的第三方模块,主要依赖的是 PIL 模块和 qrcode 库.(PIL模块只支持python2.7及以下版本,python3之后无法使用,官方推荐pyt ...

  4. python生成二维码_使用python生成二维码

    1.python-qrcode是个用来生成二维码图片的第三方模块,依赖于 PIL 模块和 qrcode 库. 首先,我们要安装三个模块,qrcode,image,PIL. pip install qr ...

  5. python制作二维码_利用Python制作二维码

    利用简单的Python代码制作二维码 友情链接:饿了么外卖大红包限时领取 一.制作工具 安装Python环境 + PyCharm编译器. 二.电脑系统 本人win10 + Python3.7.0 + ...

  6. 用python制作二维码_用python做一个可视化生成二维码的工具

    用python做一个可视化生成二维码的工具 环境 pip install gooey pip install MyQR 源代码 from gooey import GooeyParser,Gooey ...

  7. python生成二维码_用python生成二维码

    python中有一个好玩的库,不仅可以生成各种花色的二维码,还可以生成动态二维码. MyQR是一个能够生成自定义二维码的第三方库,可以根据需要生成普通二维码.带图片的艺术二维码,也可以生成动态二维码 ...

  8. python定位二维码_图像中二维码的检测和定位

    二维码 二维条码/二维码(2-dimensional bar code)是用某种特定的几何图形按一定规律在平面(二维方向上)分布的黑白相间的图形记录数据符号信息的:在代码编制上巧妙地利用构成计算机内部 ...

  9. python制作二维码_基于Python生成个性二维码过程详解

    一.问题描述 通过调用MyQR模块来实现生成个人所需二维码. 安装: pip install myqr 二.代码实现 1.普通二维码 from MyQR import myqr # 普通二维码 myq ...

最新文章

  1. CVPR2021 | 重新思考BiSeNet让语义分割模型速度起飞
  2. [Golang学习笔记] 05 程序实体2 作用域访问权限和变量重声明
  3. java父类转子类_java中什么是继承,和继承的接口的关系?
  4. 【小技巧】【牛客网】【JAVA】在线输入输出练习
  5. 小程序的 rpx布局问题
  6. POJ-1426 Find The Multiple
  7. Linux 查看命令
  8. java字符串截取指定下标位置的字符串
  9. 递归算法经典实例python-Python递归算法详解
  10. PEmicro GDB Launch Failure : Could not bind socket.
  11. QAC静态测试配置及使用教程
  12. 读《半世烟雨,半世桃花 李清照词传》有感
  13. SAP PI SLD RZ70 系统架构目录数据提供者 HTTP(S) 配置
  14. 纵横捭阖C++之从异步谈起
  15. Android O新特性和行为变更总结zz
  16. 将QTextEdit右键菜单设置为中文
  17. 如何删除电脑里的android驱动程序,【教程】安卓手机系统自带程序卸载
  18. 2016年全国高中数学联赛加试T3解答
  19. AES实现加解密-Java
  20. 后台管理系统项目-登录页-实现步骤

热门文章

  1. 五子棋 java 课设,五子棋java课程设计
  2. .nc地形数据的python转换实现tiff
  3. windows环境搭建MQTT
  4. 李敖之子李戡:《严正声明─我对韩…
  5. 如何查看电脑WIFI密码
  6. Spring源码阅读笔记(一):整体架构与核心技术
  7. rabbitMq实现延迟队列
  8. 英文不好到底能不能学会编程?
  9. IT圈子很小,遭遇人肉搜索后果很严重
  10. 条码打印软件中如何插入特殊字符