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.pswp.cn/news/390621.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390621.shtml
英文地址,請注明出處:http://en.pswp.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…