JavaScript标准对象:地图

The Map object is a relatively new standard built-in object that holds [key, value] pairs in the order that they're inserted.

Map对象是一个相对较新的标准内置对象,按插入顺序保存[key, value]对。

The keys and values in the Map object can be any value (both objects and primitive values are valid).

Map对象中的键和值可以是任何值(对象和基本值均有效)。

句法 (Syntax)

new Map([iterable])

参量 (Parameters)

iterable An Array or other iterable object whose elements are key-value pairs.

iterable一个数组或其他可迭代对象,其元素为键值对。

(Example)

const myMap = new Map();
myMap.set('foo', 1);
myMap.set('bar', 2);
myMap.set('baz', 3);myMap.get('foo');   // returns 1
myMap.get('baz');   // returns 3
myMap.get('hihi');  // return undefinedmyMap.size;   // 3console.log(myMap); // Map { 'foo' => 1, 'bar' => 2, 'baz' => 3 }

It's easy to create a new Map from existing 2D arrays of key-value pairs:

根据现有的键值对的2D数组创建新的Map很容易:

const myArr = [['foo', 1], ['bar', 2], ['baz', 3]];
const arrMap = new Map(myArr);console.log(arrMap); // Map { 'foo' => 1, 'bar' => 2, 'baz' => 3 }

You can also convert an object into a Map with the help of the Object.entries:

您还可以借助Object.entries将对象转换为Map

const myObj = {foo: 1,bar: 2,baz: 3
}
const objMap = new Map(Object.entries(myObj));console.log(objMap); // Map { 'foo' => 1, 'bar' => 2, 'baz' => 3 }

Map.prototype.get (Map.prototype.get)

Returns the value of the specified key from a Map object.

Map对象返回指定键的值。

句法 (Syntax)

myMap.get(key);

参量 (Parameters)

key Required.

密钥必填。

(Example)

const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);myMap.get('foo');   // returns 1
myMap.get('baz');   // returns 3
myMap.get('hihi');  // return undefined

Map.prototype.set (Map.prototype.set)

Sets or updates an element with the specified key and value in a Map object. The set() method also returns the Map object.

使用Map对象中的指定键和值设置或更新元素。 set()方法还返回Map对象。

句法 (Syntax)

myMap.set(key, value);

参量 (Parameters)

  • key Required

    key

  • value Required

    需要的

(Example)

const myMap = new Map();// sets new elements
myMap.set('foo', 1);
myMap.set('bar', 2);
myMap.set('baz', 3);// Updates an existing element
myMap.set('foo', 100);myMap.get('foo');   // returns 100

Because set() returns the Map object it operated on, the method can be easily chained. For example, the code above can be simplified to:

由于set()返回对其进行操作的Map对象,因此可以轻松地链接该方法。 例如,上面的代码可以简化为:

const myMap = new Map();// sets new elements
myMap.set('foo', 1).set('bar', 2).set('baz', 3).set('foo', 100); // Updates an existing elementmyMap.get('foo');   // returns 100

Map.prototype.size (Map.prototype.size)

Returns the number of elements in a Map object.

返回Map对象中的元素数。

句法 (Syntax)

myMap.size();

(Example)

const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);myMap.size(); // 3

Map.prototype.keys (Map.prototype.keys)

Returns a new Iterator object that contains the keys for each element in the Map object in insertion order.

返回一个新的Iterator对象,该对象按插入顺序包含Map对象中每个元素的键。

句法 (Syntax)

myMap.keys()

(Example)

const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);const iterator = myMap.keys();console.log(iterator.next().value); // 'foo'
console.log(iterator.next().value); // 'bar'
console.log(iterator.next().value); // 'baz'

Map.prototype.values (Map.prototype.values)

Returns an iterator object that contains the values for each element in the Map object in the order they were inserted.

返回一个迭代器对象,该迭代器对象按插入顺序包含Map对象中每个元素的值。

句法 (Syntax)

myMap.values()

(Example)

const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);const iterator = myMap.values();
console.log(iterator.next().value); // 1
console.log(iterator.next().value); // 2
console.log(iterator.next().value); // 3

Map.prototype.delete (Map.prototype.delete)

Removes the specified element from a Map object. Returns whether the key was found and successfully deleted.

Map对象移除指定的元素。 返回是否找到并成功删除密钥。

句法 (Syntax)

myMap.delete(key);

参量 (Parameters)

key Required.

密钥必填。

(Example)

const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);myMap.size(); // 3
myMap.has('foo'); // true;myMap.delete('foo');  // Returns true. Successfully removed.myMap.size(); // 2
myMap.has('foo');    // Returns false. The "foo" element is no longer present.

Map.prototype.entries (Map.prototype.entries)

Returns a new Iterator object that contains the [key, value] pairs for each element in the Map object in insertion order.

返回一个新的Iterator对象,该对象包含按插入顺序的Map对象中每个元素的[key, value]对。

句法 (Syntax)

myMap.entries()

(Example)

const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);const iterator = myMap.entries();console.log(iterator.next().value); // ['foo', 1]
console.log(iterator.next().value); // ['bar', 2]
console.log(iterator.next().value); // ['baz', 3]

Map.prototype.clear (Map.prototype.clear)

Removes all elements from a Map object and returns undefined.

Map对象移除所有元素,并返回undefined

句法 (Syntax)

myMap.clear();

(Example)

const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);myMap.size(); // 3
myMap.has('foo'); // true;myMap.clear(); myMap.size(); // 0
myMap.has('foo'); // false

Map.prototype.has (Map.prototype.has)

Given a Map with elements inside, the has() function allows you to determine whether or not an element exists inside the Map, based on a key that you pass.

给定一个Map内部包含元素, has()函数允许您根据传递的键来确定Map中是否存在元素。

The has() function returns a Boolean primitive (either true or false), which indicates that the Map contains the element or not.

has()函数返回一个Boolean原语 ( truefalse ),它指示Map是否包含该元素。

You pass a key parameter to the has() function, which will be used to look for an element with that key inside the Map.

您将key参数传递给has()函数,该函数将用于在Map中查找具有该键的元素。

Example:

例:

// A simple Map
const campers = new Map();// add some elements to the map
// each element's key is 'camp' and a number
campers.set('camp1', 'Bernardo');
campers.set('camp2', 'Andrea');
campers.set('camp3', 'Miguel');// Now I want to know if there's an element
// with 'camp4' key:
campers.has('camp4');
// output is `false`

The campers Map does not currently have an element with a 'camp4' key. Therefore, the has('camp4') function call will return false.

campers地图当前没有带有'camp4'键的元素。 因此, has('camp4')函数调用将返回false

// If we add an element with the 'camp4' key to the map
campers.set('camp4', 'Ana');// and try looking for that key again
campers.has('camp4');
// output is `true`

Since the map now does have an element with a 'camp4' key, the has('camp4') function call will return true this time!

由于地图现在确实具有带有'camp4'键的元素,因此has('camp4')函数调用这次将返回true

In a more real-world scenario, you might not manually add the elements to the Map yourself, so the has() function would become really useful in those cases.

在更真实的场景中,您可能不会自己手动将元素添加到Map中,因此has()函数在这些情况下将非常有用。

Map.prototype.forEach (Map.prototype.forEach)

Executes the passed function on each key-value pair in the Map object. Returns undefined.

Map对象中的每个键值对上执行传递的函数。 返回undefined

句法 (Syntax)

myMap.forEach(callback, thisArg)

参量 (Parameters)

  • callback Function to execute for each element.

    callback为每个元素执行的功能。

  • thisArg Value to use as this when executing callback.

    thisArg在执行回调时用作此值。

(Example)

const myMap = new Map();
myMap.set('foo',1);
myMap.set('bar',2);
myMap.set('baz',3);function valueLogger(value) {console.log(`${value}`);
}myMap.forEach(valueLogger);
// 1
// 2
// 3

翻译自: https://www.freecodecamp.org/news/javascript-standard-objects-maps/

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/390621.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

leetcode 483. 最小好进制

题目 对于给定的整数 n, 如果n的k(k>2)进制数的所有数位全为1,则称 k(k>2)是 n 的一个好进制。 以字符串的形式给出 n, 以字符串的形式返回 n 的最小好进制。 示例 1: 输入:“13” 输…

图像灰度变换及图像数组操作

Python图像灰度变换及图像数组操作 作者:MingChaoSun 字体:[增加 减小] 类型:转载 时间:2016-01-27 我要评论 这篇文章主要介绍了Python图像灰度变换及图像数组操作的相关资料,需要的朋友可以参考下使用python以及numpy通过直接操…

npx npm区别_npm vs npx —有什么区别?

npx npm区别If you’ve ever used Node.js, then you must have used npm for sure.如果您曾经使用过Node.js ,那么一定要使用npm 。 npm (node package manager) is the dependency/package manager you get out of the box when you install Node.js. It provide…

找出性能消耗是第一步,如何解决问题才是关键

作者最近刚接手一个新项目,在首页列表滑动时就感到有点不顺畅,特别是在滑动到有 ViewPager 部分的时候,如果是熟悉的项目,可能会第一时间会去检查代码,但前面说到这个是刚接手的项目,同时首页的代码逻辑比较…

bigquery_如何在BigQuery中进行文本相似性搜索和文档聚类

bigqueryBigQuery offers the ability to load a TensorFlow SavedModel and carry out predictions. This capability is a great way to add text-based similarity and clustering on top of your data warehouse.BigQuery可以加载TensorFlow SavedModel并执行预测。 此功能…

bzoj 1996: [Hnoi2010]chorus 合唱队

Description 为了在即将到来的晚会上有吏好的演出效果&#xff0c;作为AAA合唱队负责人的小A需要将合唱队的人根据他们的身高排出一个队形。假定合唱队一共N个人&#xff0c;第i个人的身髙为Hi米(1000<Hi<2000),并已知任何两个人的身高都不同。假定最终排出的队形是A 个人…

移动应用程序开发_什么是移动应用程序开发?

移动应用程序开发One of the most popular forms of coding in the last decade has been the creation of apps, or applications, that run on mobile devices.在过去的十年中&#xff0c;最流行的编码形式之一是创建在移动设备上运行的应用程序。 Today there are two main…

leetcode 1600. 皇位继承顺序(dfs)

题目 一个王国里住着国王、他的孩子们、他的孙子们等等。每一个时间点&#xff0c;这个家庭里有人出生也有人死亡。 这个王国有一个明确规定的皇位继承顺序&#xff0c;第一继承人总是国王自己。我们定义递归函数 Successor(x, curOrder) &#xff0c;给定一个人 x 和当前的继…

vlookup match_INDEX-MATCH — VLOOKUP功能的升级

vlookup match电子表格/索引匹配 (SPREADSHEETS / INDEX-MATCH) In a previous article, we discussed about how and when to use VLOOKUP functions and what are the issues that we might face while using them. This article, on the other hand, will take you to a jou…

java基础-BigDecimal类常用方法介绍

java基础-BigDecimal类常用方法介绍 作者&#xff1a;尹正杰 版权声明&#xff1a;原创作品&#xff0c;谢绝转载&#xff01;否则将追究法律责任。 一.BigDecimal类概述 我们知道浮点数的计算结果是未知的。原因是计算机二进制中&#xff0c;表示浮点数不精确造成的。这个时候…

节点对象转节点_节点流程对象说明

节点对象转节点The process object in Node.js is a global object that can be accessed inside any module without requiring it. There are very few global objects or properties provided in Node.js and process is one of them. It is an essential component in the …

PAT——1018. 锤子剪刀布

大家应该都会玩“锤子剪刀布”的游戏&#xff1a;两人同时给出手势&#xff0c;胜负规则如图所示&#xff1a; 现给出两人的交锋记录&#xff0c;请统计双方的胜、平、负次数&#xff0c;并且给出双方分别出什么手势的胜算最大。 输入格式&#xff1a; 输入第1行给出正整数N&am…

leetcode 1239. 串联字符串的最大长度

题目 二进制手表顶部有 4 个 LED 代表 小时&#xff08;0-11&#xff09;&#xff0c;底部的 6 个 LED 代表 分钟&#xff08;0-59&#xff09;。每个 LED 代表一个 0 或 1&#xff0c;最低位在右侧。 例如&#xff0c;下面的二进制手表读取 “3:25” 。 &#xff08;图源&am…

flask redis_在Flask应用程序中将Redis队列用于异步任务

flask redisBy: Content by Edward Krueger and Josh Farmer, and Douglas Franklin.作者&#xff1a; 爱德华克鲁格 ( Edward Krueger) 和 乔什法默 ( Josh Farmer )以及 道格拉斯富兰克林 ( Douglas Franklin)的内容 。 When building an application that performs time-co…

CentOS7下分布式文件系统FastDFS的安装 配置 (单节点)

背景 FastDFS是一个开源的轻量级分布式文件系统&#xff0c;为互联网量身定制&#xff0c;充分考虑了冗余备份、负载均衡、线性扩容等机制&#xff0c;并注重高可用、高性能等指标&#xff0c;解决了大容量存储和负载均衡的问题&#xff0c;特别适合以文件为载体的在线服务&…

如何修复会话固定漏洞_PHP安全漏洞:会话劫持,跨站点脚本,SQL注入以及如何修复它们...

如何修复会话固定漏洞PHP中的安全性 (Security in PHP) When writing PHP code it is very important to keep the following security vulnerabilities in mind to avoid writing insecure code.在编写PHP代码时&#xff0c;记住以下安全漏洞非常重要&#xff0c;以避免编写不…

剑指 Offer 38. 字符串的排列

题目 输入一个字符串&#xff0c;打印出该字符串中字符的所有排列。 你可以以任意顺序返回这个字符串数组&#xff0c;但里面不能有重复元素。 示例: 输入&#xff1a;s “abc” 输出&#xff1a;[“abc”,“acb”,“bac”,“bca”,“cab”,“cba”] 限制&#xff1a; 1…

前馈神经网络中的前馈_前馈神经网络在基于趋势的交易中的有效性(1)

前馈神经网络中的前馈This is a preliminary showcase of a collaborative research by Seouk Jun Kim (Daniel) and Sunmin Lee. You can find our contacts at the bottom of the article.这是 Seouk Jun Kim(Daniel) 和 Sunmin Lee 进行合作研究的初步展示 。 您可以在文章底…

解释什么是快速排序算法?_解释排序算法

解释什么是快速排序算法?Sorting algorithms are a set of instructions that take an array or list as an input and arrange the items into a particular order.排序算法是一组指令&#xff0c;这些指令采用数组或列表作为输入并将项目按特定顺序排列。 Sorts are most c…

SpringBoot自动化配置的注解开关原理

我们以一个最简单的例子来完成这个需求&#xff1a;定义一个注解EnableContentService&#xff0c;使用了这个注解的程序会自动注入ContentService这个bean。 Retention(RetentionPolicy.RUNTIME) Target(ElementType.TYPE) Import(ContentConfiguration.class) public interfa…