es6写名字写上对应年龄
时间: 2023-08-03 16:14:56 浏览: 44
可以使用ES6的新特性Map来实现。代码如下:
```
const nameAgeMap = new Map([
['Tom', 20],
['Jerry', 25],
['Alice', 18]
]);
nameAgeMap.forEach((age, name) => console.log(`${name}的年龄是${age}`));
```
输出结果:
```
Tom的年龄是20
Jerry的年龄是25
Alice的年龄是18
```
相关问题
es6手写几个数组去重
### 回答1:
在ES6中,可以采用以下几种方法来手写数组去重:
1. 使用Set数据结构:
使用Set数据结构是一种简单且高效的方法,由于Set不允许重复值,因此可以利用这一特性快速实现数组去重:
```javascript
const arr = [1, 2, 3, 4, 5, 1, 2, 3];
const uniqueArr = [...new Set(arr)];
```
2. 使用filter方法:
利用数组的filter方法结合indexOf可以实现数组去重。
```javascript
const arr = [1, 2, 3, 4, 5, 1, 2, 3];
const uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index);
```
3. 使用reduce方法:
利用reduce方法遍历数组,将不重复的值保存在一个新的数组中。
```javascript
const arr = [1, 2, 3, 4, 5, 1, 2, 3];
const uniqueArr = arr.reduce((acc, cur) => {
if (!acc.includes(cur)) {
acc.push(cur);
}
return acc;
}, []);
```
这些方法都可以在ES6中手写实现数组去重,具体选择哪种方法取决于实际的需求和性能要求。
### 回答2:
ES6提供了几种去重数组的方法,下面我将手写几个常用的数组去重方法。
方法一:使用Set对象
```javascript
let arr = [1, 2, 3, 3, 4, 4, 5];
let uniqueArr = Array.from(new Set(arr));
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
方法二:使用filter函数和indexOf方法
```javascript
let arr = [1, 2, 3, 3, 4, 4, 5];
let uniqueArr = arr.filter((item, index) => arr.indexOf(item) === index);
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
方法三:使用reduce函数
```javascript
let arr = [1, 2, 3, 3, 4, 4, 5];
let uniqueArr = arr.reduce((prev, curr) => prev.includes(curr) ? prev : [...prev, curr], []);
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
方法四:使用利用Map对象
```javascript
let arr = [1, 2, 3, 3, 4, 4, 5];
let map = new Map();
arr.forEach(item => map.set(item, item));
let uniqueArr = [...map.values()];
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
以上就是我手写的几个利用ES6实现数组去重的方法。这些方法都能有效地去除数组中的重复元素。
### 回答3:
在ES6中,有许多方法可以用来手写数组去重。以下是其中几种常见的方法:
1. 使用Set对象:ES6中新增的Set对象可以用来存储唯一的值。可以将数组转换为Set对象,然后将Set对象再转换回数组,即可实现数组的去重。
```javascript
const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArr = Array.from(new Set(arr));
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
2. 使用reduce方法:使用reduce方法遍历数组,将每个元素添加到一个空数组中,但之前先检查该元素是否已存在于新数组中。
```javascript
const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArr = arr.reduce((acc, cur) => {
if (!acc.includes(cur)) {
acc.push(cur);
}
return acc;
}, []);
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
3. 使用filter方法:使用filter方法遍历数组,根据元素在数组中的索引位置进行判断,只保留第一次出现的元素。
```javascript
const arr = [1, 2, 2, 3, 4, 4, 5];
const uniqueArr = arr.filter((value, index, self) => {
return self.indexOf(value) === index;
});
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
这些方法都可以有效地实现数组的去重,根据实际情况选择适合的方法使用即可。
用ES6写一个数组去重
我们可以使用ES6中的Set对象来进行数组去重,这是Set对象的一个特性,它只会保存不重复的值。
```javascript
const arr = [1, 2, 3, 3, 4, 4, 5];
const uniqueArr = [...new Set(arr)];
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
上面的代码中,我们先创建了一个数组`arr`,其中包含了重复的元素。然后,我们使用`new Set(arr)`创建了一个Set对象,这个对象只包含不重复的元素。最后,我们使用展开运算符`[... ]`将Set对象转换为数组,得到了去重后的数组`uniqueArr`。
也可以使用Array.from()方法来将Set对象转换为数组:
```javascript
const arr = [1, 2, 3, 3, 4, 4, 5];
const uniqueArr = Array.from(new Set(arr));
console.log(uniqueArr); // [1, 2, 3, 4, 5]
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)