Home > Web Front-end > JS Tutorial > ES6 set data structure and map data structure

ES6 set data structure and map data structure

php中世界最好的语言
Release: 2018-03-10 15:04:08
Original
1843 people have browsed it

This time I will bring you the set data structure and map data structure of ES6. Using the set data structure and map data structure of ES6What are the precautions?The following is a practical case, let's take a look.

set data structure: (each item is different)

API: add(); delete();clear();has();sizeAttributes ;

let arr=["wowo","hh","web"];
let setArr = new Set(arr);
console.log(setArr );      //Set(3) {"wowo", "hh", "web"}
typeof setArr                  // object
setArr.add("呵呵哒");
console.log(setArr );      //Set(4) {"wowo", "hh", "web","呵呵哒"}
                                      //其实追加的位置并没有顺序;
setArr.delete("wowo");
console.log(setArr);      //Set(3) { "hh", "web","呵呵哒"}
Copy after login

clear();will condition all contents;has returns true or false;

Use for of or foreach() to traverse the set data structure;

WeakSet data structure;

You need to declare the object first and then add it; otherwise an error will be reported; the weakSet data structure can only store objects and cannot be traversed;

Map data Structure: (API set( ); get( ); delete( ); has(); clear(); size() )

Let’s first look at ordinary json objects:

let json ={"name":"liuliu","age":23};

console.log(json.name); In this process, the value of name is obtained, and each value in json will be looped Attribute;

But map is different, it is stored according to mapping, and key and val can be any otherdata type:

let map =new Map();
map.set("age","22");
map.get("age") //22;
map.has("age") //true;
map.delete("age");
map.size;        //0;
Copy after login

I believe you have read the case in this article You have mastered the method. For more exciting information, please pay attention to other related articles on the php Chinese website!

Related reading:

Detailed explanation of string templates in ES6

Detailed explanation of destructuring assignment in ES6

Detailed explanation of scope and declaration of variables in ES6

The above is the detailed content of ES6 set data structure and map data structure. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template