# ES6基础

* [ECMAScript](/directory-2/es6-ji-chu.md#ecmascript)
  * [数组](https://note.niefee.com/directory-2/pages/-LjTb6f5WGR_UyaNmV-a#数组)
    * [新方法](https://note.niefee.com/directory-2/pages/-LjTb6f5WGR_UyaNmV-a#新方法)
    * [循环遍历](https://note.niefee.com/directory-2/pages/-LjTb6f5WGR_UyaNmV-a#循环遍历)
  * [数据结构](https://note.niefee.com/directory-2/pages/-LjTb6f5WGR_UyaNmV-a#数据结构)
    * [set()](/directory-2/es6-ji-chu.md#set)
    * [map()](/directory-2/es6-ji-chu.md#map)
    * [转数组](https://note.niefee.com/directory-2/pages/-LjTb6f5WGR_UyaNmV-a#转数组)

## ECMAScript

### 数组

#### 新方法

* Array.from()

将类数组转换成数组。

```javascript
let arrayLike = {
    '0': 'a',
    '1': 'b',
    '2': 'c',
    length: 3
};

// ES5的写法
var arr1 = [].slice.call(arrayLike); // ['a', 'b', 'c']

// ES6的写法
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
```

* Array.of()

Array.of方法用于将一组值，转换为数组。

```javascript
Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1
```

#### 循环遍历

```javascript
var str="muNi";

for(var value of str){
    console.log(v);
}

var str="muNi";

//对键值进行遍历
for(var value of str){
    console.log(value);
}

//对key值进行遍历
for(var key of str){
    console.log(key);
}

//对键名键值遍历
for(var [k,v] of str.entries()){
    console.log(k,v);
}
```

### 数据结构

#### set()

```javascript
"use strict"

var set=new Set([1,2,2,3,4,5]);
console.log(set);

//Set { 1, 2, 3, 4, 5 }
//去掉重复

//console.log(set.size);
//5
```

**方法**

* add()
  * 添加内容
* delete()
  * 删除
* has()
  * 查找
* clear()
  * 全部清除

它是类数组，但成员值是唯一的。

#### map()

```javascript
var mm=new Map([["name","leo"],["age","40"]]);
```

**方法**

* set()
  * 添加数值
* get()
  * 访问数值
* has(key)
* delete()
* clear()

#### 转数组

```javascript
var arr=[...map];
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://note.niefee.com/directory-2/es6-ji-chu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
