-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// 语法: new Map() | ||
|
||
let myMap = new Map(); | ||
|
||
let keyObj = {}; | ||
let keyFunc = function () {}; | ||
let keyString = 'a string'; | ||
|
||
myMap.set(keyString, '和键\'a string\'关联的值'); | ||
myMap.set(keyObj, '和键keyObj关联的值'); | ||
myMap.set(keyFunc, '和键keyFunc关联的值'); | ||
|
||
console.log(myMap.get(keyString)); | ||
console.log(myMap.get({})); //undefined | ||
console.log(myMap.get(() => {})); //undefined | ||
|
||
//NAN作为map键 | ||
let myMap2 = new Map(); | ||
myMap2.set(NaN, 'not a number'); | ||
// console.log(myMap2.get(NaN)); // "not a number" | ||
|
||
let otherNaN = Number("foo"); | ||
// console.log(otherNaN); | ||
// console.log(myMap2.get(otherNaN)); // "not a number" | ||
|
||
//for of 迭代Map | ||
for(let [key,value] of myMap){ | ||
console.log(key,value); | ||
} | ||
|
||
/** | ||
* Map与数组互转 new Map() <=> [...map] Array.from(map) | ||
*/ | ||
let kvArray = [["key1", "value1"], ["key2", "value2"]]; | ||
let myApp3 = new Map(kvArray); | ||
console.log(myApp3.get("key1")); | ||
let array1 = Array.from(myApp3); | ||
let array2 = [...myApp3]; | ||
console.log(array1,array2); | ||
console.log(Array.isArray(array1),Array.isArray(array2)); | ||
|
||
/** | ||
* 数组合并 | ||
*/ | ||
let first = new Map([[1, 'one'], [2, 'two'], [3, 'three'],]); | ||
|
||
let second = new Map([[1, 'uno'], [2, 'dos']]); | ||
|
||
// 合并两个Map对象时,如果有重复的键值,则后面的会覆盖前面的。 | ||
// 展开运算符本质上是将Map对象转换成数组。 | ||
let merged = new Map([...first, ...second]); | ||
|
||
console.log(merged.get(1)); // uno | ||
console.log(merged.get(2)); // dos | ||
console.log(merged.get(3)); // three | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* 语法:new Set([iterable]); | ||
*/ | ||
|
||
let mySet = new Set(); | ||
|
||
mySet.add(1); | ||
mySet.add(5); | ||
mySet.add(5); | ||
mySet.add('some text'); | ||
let o = { | ||
a: 1, | ||
b: 2, | ||
}; | ||
mySet.add(o); | ||
mySet.add(o); | ||
|
||
// console.log(mySet); | ||
|
||
mySet.has(1); //true | ||
mySet.has(o); //true | ||
|
||
// mySet.delete(o); //true,删除o | ||
// mySet.size; //3 | ||
|
||
/** | ||
* Set与Array互转 | ||
*/ | ||
let mySet2 = new Set([1, 2, 3, 4, { | ||
a: 1, | ||
b: 2, | ||
}]); | ||
let arr = [...mySet2]; | ||
|
||
/** | ||
* set1与set2求交集 | ||
*/ | ||
let x = new Set([...mySet].filter(v => mySet2.has(v))); | ||
console.log(x); | ||
|
||
/** | ||
* set1与set2求并集 | ||
*/ | ||
let difference = new Set([...mySet].filter(v => !mySet2.has(v))); | ||
|
||
let x1 = new Set([{id:1},{id:2}]) | ||
let x2 = new Set([{id:1},{id:3}]) | ||
let x3 = new Set([...x1].filter(v => !x2.has(v))); | ||
console.log(x3); | ||
|
||
let a1 = [{id:1},{id:2}] | ||
let a2 = [{id:1},{id:3}] | ||
for(const item1 of a2){ //循环json数组对象的内容 | ||
|
||
let flag = true; //建立标记,判断数据是否重复,true为不重复 | ||
|
||
for(const item2 of a1){ //循环新数组的内容 | ||
|
||
if(item1.id==item2.id){ //让json数组对象的内容与新数组的内容作比较,相同的话,改变标记为false | ||
flag = false; | ||
} | ||
|
||
} | ||
|
||
if(flag){ //判断是否重复 | ||
a1.push(item1); //不重复的放入新数组。 新数组的内容会继续进行上边的循环。 | ||
} | ||
|
||
|
||
} | ||
console.log(a1); | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|