sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  1796 随笔 :: 22 文章 :: 24 评论 :: 226万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

js Map对象的用法

第一篇:

Map:

Map是一组键值对的结构,具有极快的查找速度。

举个例子,假设要根据同学的名字查找对应的成绩,如果用Array实现,需要两个Array

var names = ['Michael', 'Bob', 'Tracy'];
var scores = [95, 75, 85];

给定一个名字,要查找对应的成绩,就先要在names中找到对应的位置,再从scores取出对应的成绩,Array越长,耗时越长。

如果用Map实现,只需要一个“名字”-“成绩”的对照表,直接根据名字查找成绩,无论这个表有多大,查找速度都不会变慢。用JavaScript写一个Map如下:

var m = new Map([['Michael', 95], ['Bob', 75], ['Tracy', 85]]);

m.get('Michael'); // 95

初始化Map需要一个二维数组,或者直接初始化一个空MapMap具有以下方法:

复制代码
复制代码
var m = new Map(); // 空Map

m.set('Adam', 67); // 添加新的key-value

m.set('Bob', 59);

m.has('Adam'); // 是否存在key 'Adam': true

m.get(
'Adam'); // 67

m.
delete('Adam'); // 删除key 'Adam'

m.get(
'Adam'); // undefined

复制代码
复制代码

由于一个key只能对应一个value,所以,多次对一个key放入value,后面的值会把前面的值冲掉:

复制代码
复制代码
var m = new Map();

m.set('Adam', 67);

m.set('Adam', 88);

m.get('Adam'); // 88

复制代码
复制代码

Set:

SetMap类似,也是一组key的集合,但不存储value。由于key不能重复,所以,在Set中,没有重复的key。

要创建一个Set,需要提供一个Array作为输入,或者直接创建一个空Set

var s1 = new Set(); // 空Set

var s2 = new Set([1, 2, 3]); // 含1, 2, 3

重复元素在Set中自动被过滤:

var s = new Set([1, 2, 3, 3, '3']);

s; // Set {1, 2, 3, "3"}

注意数字3和字符串'3'是不同的元素。

通过add(key)方法可以添加元素到Set中,可以重复添加,但不会有效果:

复制代码
复制代码
s.add(4);

s; // Set {1, 2, 3, 4}

s.add(
4);

s; // 仍然是 Set {1, 2, 3, 4}

复制代码
复制代码

通过delete(key)方法可以删除元素:

复制代码
复制代码
var s = new Set([1, 2, 3]);

s; // Set {1, 2, 3}

s.
delete(3);

s; // Set {1, 2}

复制代码
复制代码

小结

MapSet是ES6标准新增的数据类型,请根据浏览器的支持情况决定是否要使用

Set和Map主要的应用场景在于数组去重数据存储

Set是一种叫做集合的数据结构,Map是一种叫做字典的数据结构

集合

  • 集合是由一组无序且唯一(即不能重复)的项组成的,可以想象成集合是一个既没有重复元素,也没有顺序概念的数组
  • ES6提供了新的数据结构Set。它类似于数组,但是成员的值都是唯一的,没有重复的值
  • Set 本身是一个构造函数,用来生成 Set 数据结构
  • 这里说的Set其实就是我们所要讲到的集合,先来看下基础用法
复制代码
复制代码
const s = new Set();
[
2, 3, 5, 4, 5, 2, 2].forEach(x => s.add(x));
for (let i of s) { console.log(i); // 2 3 5 4 } // 去除数组的重复成员 let array = [1,2,1,4,5,3];

[...new Set(array)] // [1, 2, 4, 5, 3]

复制代码
复制代码

Set实例的属性和方法

  • Set的属性:
    • size:返回集合所包含元素的数量
  • Set的方法:
    • 操作方法
      • add(value):向集合添加一个新的项
      • delete(value):从集合中移除一个值
      • has(value):如果值在集合中存在,返回true,否则false
      • clear(): 移除集合里所有的项
    • 遍历方法
      • keys():返回一个包含集合中所有键的数组
      • values():返回一个包含集合中所有值的数组
      • entries:返回一个包含集合中所有键值对的数组(感觉没什么用就不实现了)
      • forEach():用于对集合成员执行某种操作,没有返回值

创建一个集合

复制代码
复制代码
function Set(arr = []) {    // 可以传入数组

    let items = {};
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0;  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 记录集合中成员的数量</span>
} module.exports = Set;

复制代码

复制代码
复制代码

这里用{}对象来表示集合,也是因为对象不允许一个键指向两个不同的属性,保证了集合里的元素都是唯一的

接下来,就需要按照ES6中Set类的实现,添加一些集合的操作方法

has方法

首先要实现的是has方法,因为在add和delete等其他方法中都会被调用,下面来看一下它的实现

复制代码
复制代码
function Set() {
let items </span>=<span style="color: rgba(0, 0, 0, 1)"> {};

</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> has(val)方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.has = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(val) {

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 对象都有hasOwnProperty方法,判断是否拥有特定属性</span>

    <span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> items.hasOwnProperty(val);  

};

}

复制代码
复制代码

add方法

接下来要实现add方法

复制代码
复制代码
    // add(val)方法
<span style="color: rgba(0, 0, 255, 1)">this</span>.add = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(val) {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        items[val] </span>=<span style="color: rgba(0, 0, 0, 1)"> val;

        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.size++;    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 累加集合成员数量</span>

        <span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;
    }
    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;
};

复制代码
复制代码

对于给定的val,可以检测是否存在于集合中

  • 如果不存在,就添加到集合中,返回true
  • 如果存在,就直接返回false,不做任何操作

delete和clear方法

继续写着,这回把两个都写上

复制代码
复制代码
    // delete(val)方法
<span style="color: rgba(0, 0, 255, 1)">this</span>.<span style="color: rgba(0, 0, 255, 1)">delete</span> = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(val) {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        </span><span style="color: rgba(0, 0, 255, 1)">delete</span> items[val];  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将items对象上的属性删掉</span>

        <span style="color: rgba(0, 0, 255, 1)">this</span>.size--<span style="color: rgba(0, 0, 0, 1)">;

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> clear方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.clear = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {

    items </span>= {};     <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 直接将集合赋一个空对象即可</span>

    <span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

};</span></pre>
复制代码
复制代码

在delete方法中,判断val是否存在于集合中,如果存在就直接从集合中删掉,返回true

以上完成的都是操作方法,下面我们再来实现一下遍历方法

keys、values方法

这两个方法我们可以放在一起来实现,因为通过ES6对Object的扩展可以轻松实现对应的方法,下面看一下具体实现,上代码:

复制代码
复制代码
    // keys()方法
<span style="color: rgba(0, 0, 255, 1)">this</span>.keys = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> Object.keys(items);  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回遍历集合的所有键名的数组</span>
};
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> values()方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.values = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">() {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> Object.values(items);  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 返回遍历集合的所有键值的数组</span>
};
复制代码
复制代码

使用一下看看

复制代码
复制代码
// set.js

const Set = require('./Set.js');    // 导入写好的Set类

let set = new Set();

set.add(1);

set.add(3);

set.add(2);

console.log(set.keys()); // [ '1', '2', '3' ]

console.log(set.values());
// [ 1, 2, 3 ]

复制代码

复制代码
复制代码

这里我们看到和ES6中的Set有点区别,因为Object的这几个方法都是按照数值大小,从小到大遍历的数组,所以大家知道这一点比较好,具体实现还是有些不同的,哈哈

forEach方法

ES6中Set结构的实例上带的forEach方法,其实和数组的forEach方法很相似,只不过Set结构的键名就是键值,所以第一个参数与第二个参数的值永远都是一样的

下面就按照实现数组的forEach方法,我们来完成Set的forEach方法

复制代码
复制代码
// forEach(fn, context)方法
<span style="color: rgba(0, 0, 255, 1)">this</span>.forEach = <span style="color: rgba(0, 0, 255, 1)">function</span>(fn, context = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">) {

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; <span style="color: rgba(0, 0, 255, 1)">this</span>.size; i++<span style="color: rgba(0, 0, 0, 1)">) {

        let item </span>=<span style="color: rgba(0, 0, 0, 1)"> Object.keys(items)[i];

        fn.call(context, item, item, items);     

    }

};</span></pre>
复制代码
复制代码

使用forEach方法

复制代码
复制代码
// set.js

const Set = require('./Set.js');

let set = new Set();

set.add(1);

set.add(4);

set.add('3');

set.forEach((value, key) => console.log(key + ' : ' + value)); // 1:1, 3:3, 4:4

let arr
= set.values(); // [ 1, 3, 4 ]

arr
= new Set(arr.map(x => x * 2)).values();

console.log(arr); // [ 2, 6, 8 ]

复制代码
复制代码

基本上实现了Set结构的方法,不过,发现一个问题,那就是每次添加一个元素都要add这样写起来确实好麻烦,Set是可以接收一个数组作为参数的,那么我们把这个也实现一下

复制代码
复制代码
function Set(arr = []) {    // 传入接受的数组,如果没有传指定一个空数组做为初始值

    let items = {};
</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> has方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.has = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> items.hasOwnProperty(val);

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> add方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.add = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果没有存在items里面就可以直接写入</span>

    <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        items[val] </span>=<span style="color: rgba(0, 0, 0, 1)"> val;

        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.size++<span style="color: rgba(0, 0, 0, 1)">;

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

};

arr.forEach((val, i) </span>=&gt; {   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 遍历传入的数组</span>

    <span style="color: rgba(0, 0, 255, 1)">this</span>.add(val);          <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将数组里每一项值添加到集合中</span>
});
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 省略...</span>
}
复制代码
复制代码
复制代码
复制代码
function Set(arr = []) {
let items </span>=<span style="color: rgba(0, 0, 0, 1)"> {};

</span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> has方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.has = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> items.hasOwnProperty(val);

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> add方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.add = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 如果没有存在items里面就可以直接写入</span>

    <span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        items[val] </span>=<span style="color: rgba(0, 0, 0, 1)"> val;

        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.size++<span style="color: rgba(0, 0, 0, 1)">;

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

};

arr.forEach((val, i) </span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {

    </span><span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.add(val);

});

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> delete方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.<span style="color: rgba(0, 0, 255, 1)">delete</span> = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (val) {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.has(val)) {

        </span><span style="color: rgba(0, 0, 255, 1)">delete</span> items[val];  <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将items对象上的属性删掉</span>

        <span style="color: rgba(0, 0, 255, 1)">this</span>.size--<span style="color: rgba(0, 0, 0, 1)">;

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> clear方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.clear = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {

    items </span>=<span style="color: rgba(0, 0, 0, 1)"> {};

    </span><span style="color: rgba(0, 0, 255, 1)">this</span>.size = 0<span style="color: rgba(0, 0, 0, 1)">;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> keys方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.keys = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Object.keys(items);

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> values方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.values = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> () {

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> Object.values(items);

}

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> forEach方法</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.forEach = <span style="color: rgba(0, 0, 255, 1)">function</span> (fn, context = <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">) {

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; <span style="color: rgba(0, 0, 255, 1)">this</span>.size; i++<span style="color: rgba(0, 0, 0, 1)">) {

        let item </span>=<span style="color: rgba(0, 0, 0, 1)"> Object.keys(items)[i];

        fn.call(context, item, item, items);

    }

}



</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 并集</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.union = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (other) {

    let union </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Set();

    let values </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.values();



    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

        union.add(values[i]);

    }

    values </span>= other.values();    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 将values重新赋值为新的集合</span>

    <span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

        union.add(values[i]);

    }



    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> union;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 交集</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.intersect = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (other) {

    let intersect </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Set();

    let values </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.values();

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

        </span><span style="color: rgba(0, 0, 255, 1)">if</span><span style="color: rgba(0, 0, 0, 1)"> (other.has(values[i])) {

            intersect.add(values[i]);

        }

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> intersect;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 差集</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.difference = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (other) {

    let difference </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Set();

    let values </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.values();

    </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

        </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">other.has(values[i])) {

            difference.add(values[i]);

        }

    }

    </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> difference;

};

</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 子集</span>

<span style="color: rgba(0, 0, 255, 1)">this</span>.subset = <span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(other) {

    </span><span style="color: rgba(0, 0, 255, 1)">if</span> (<span style="color: rgba(0, 0, 255, 1)">this</span>.size &gt;<span style="color: rgba(0, 0, 0, 1)"> other.size) {

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

    } </span><span style="color: rgba(0, 0, 255, 1)">else</span><span style="color: rgba(0, 0, 0, 1)"> {

        let values </span>= <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.values();

        </span><span style="color: rgba(0, 0, 255, 1)">for</span> (let i = 0; i &lt; values.length; i++<span style="color: rgba(0, 0, 0, 1)">) {

            console.log(values[i])

            console.log(other.values())

            </span><span style="color: rgba(0, 0, 255, 1)">if</span> (!<span style="color: rgba(0, 0, 0, 1)">other.has(values[i])) {

                </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">;

            }

        }

        </span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">;

    }

};

}

module.exports = Set;

复制代码
复制代码
复制代码
复制代码
const Set = require('./Set.js');

let set = new Set([2, 1, 3]);

console.log(set.keys()); // [ '1', '2', '3' ]

console.log(set.values());
// [ 1, 2, 3 ]

console.log(set.size);
// 3

set.
delete(1);

console.log(set.values()); // [ 2, 3 ]

set.clear();

console.log(set.size); // 0

// 并集

let a
= [1, 2, 3];

let b = new Set([4, 3, 2]);

let union = new Set(a).union(b).values();

console.log(union); // [ 1, 2, 3, 4 ]

// 交集

let c
= new Set([4, 3, 2]);

let intersect = new Set([1,2,3]).intersect(c).values();

console.log(intersect); // [ 2, 3 ]

// 差集

let d
= new Set([4, 3, 2]);

let difference = new Set([1,2,3]).difference(d).values();

// [1,2,3]和[4,3,2]的差集是1

console.log(difference);
// [ 1 ]

复制代码
复制代码

ES6为Array增加了from函数用来将其他对象转换成数组。

当然,其他对象也是有要求,也不是所有的,可以将两种对象转换成数组。

1.部署了Iterator接口的对象,比如:Set,Map,Array。

2.类数组对象,什么叫类数组对象,就是一个对象必须有length属性,没有length,转出来的就是空数组。

转换map

将Map对象的键值对转换成一个一维数组。

实际上转换出来的数组元素的序列是key1,value1,key2,value2,key3,value3.....

const map1 = new Map();
map1.set('k1', 1);
map1.set('k2', 2);
map1.set('k3', 3);
console.log('%s', Array.from(map1))

结果:

k1,1,k2,2,k3,3

转换set

将Set对象的元素转换成一个数组。

const set1 = new Set();
set1.add(1).add(2).add(3)
console.log('%s', Array.from(set1))

结果

1,2,3

转换字符串

可以吧ascii的字符串拆解成一个数据,也可以准确的将unicode字符串拆解成数组。

console.log('%s', Array.from('hello world'))
console.log('%s', Array.from('\u767d\u8272\u7684\u6d77'))

结果:

h,e,l,l,o, ,w,o,r,l,d
白,色,的,海

类数组对象

一个类数组对象必须要有length,他们的元素属性名必须是数值或者可以转换成数值的字符。

注意:属性名代表了数组的索引号,如果没有这个索引号,转出来的数组中对应的元素就为空。

console.log('%s', Array.from({
  0: '0',
  1: '1',
  3: '3',
  length:4
}))

结果:

0,1,,3

如果对象不带length属性,那么转出来就是空数组。

console.log('%s', Array.from({
  0: 0,
  1: 1
}))

结果就是空数组。

对象的属性名不能转换成索引号时。

console.log('%s', Array.from({
  a: '1',
  b: '2',
  length:2
}))

结果也是空数组

Array.from可以接受三个参数

我们看定义:

Array.from(arrayLike[, mapFn[, thisArg]])

arrayLike:被转换的的对象。

mapFn:map函数。

thisArg:map函数中this指向的对象。

第二个参数,map函数

用来对转换中,每一个元素进行加工,并将加工后的结果作为结果数组的元素值。

console.log('%s', Array.from([1, 2, 3, 4, 5], (n) => n + 1))

结果:

上面的map函数实际上是给数组中的每个数值加了1。

2,3,4,5,6

第三个参数,map函数中this指向的对象

该参数是非常有用的,我们可以将被处理的数据和处理对象分离,将各种不同的处理数据的方法封装到不同的的对象中去,处理方法采用相同的名字。

在调用Array.from对数据对象进行转换时,可以将不同的处理对象按实际情况进行注入,以得到不同的结果,适合解耦。

这种做法是模板设计模式的应用,有点类似于依赖注入。

复制代码

let diObj = {
  handle: function(n){
    return n + 2
  }
}

console.log('%s', Array.from(
[1, 2, 3, 4, 5],
function (x){
return this.handle(x)
},
diObj))

复制代码

结果:

3,4,5,6,7
https://www.cnblogs.com/yuer20180726/p/11387699.html
posted on   sunny123456  阅读(840)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示