首页 > 其他分享 >兼收并蓄 TypeScript - 基础: set

兼收并蓄 TypeScript - 基础: set

时间:2024-09-20 12:15:37浏览次数:1  
标签:TypeScript console log mySet 兼收并蓄 set new Set

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

兼收并蓄 TypeScript - 基础: set

示例如下:

basic\set.ts

{
    // set 是一个集合,先进先出,不会插入重复数据,数据支持类型的多样性
    // 常规操作有 add(), delete(), has(), clear(), size 等
    let mySet = new Set();


    mySet.add(1);
    mySet.add(2);
    mySet.add("webabcd"); // 数据支持类型的多样性
    mySet.add(2); // 不会插入重复数据


    // 遍历
    for (let value of mySet) {
        console.log(value);
    }
    // 遍历
    mySet.forEach(function(value) {
        console.log(value);
    });


    // set 对象转数组
    console.log(mySet); // {1, 2, webabcd}
    console.log([...mySet]); // [1, 2, 'webabcd']


    // 实例化 set 对象时,初始化其数据集合
    console.log(new Set(["1", "2", "3"])); // {1, 2, 3}


    // string 转 set
    console.log(new Set("webabcd")); // {"w", "e", "b", "a", "b", "c", "d"}


    // set 可以给数组排重
    console.log(new Set([3, 1, 2, 3, 2])); // {3, 1, 2}


    let a = new Set([1, 2, 3]);
    let b = new Set([4, 3, 2]);
    // set 可以合并,结果排重
    console.log(new Set([...a, ...b])); // {1, 2, 3, 4}
    // set 可以合并,结果取交集(通过 array 的 filter 实现)
    console.log(new Set([...a].filter(p => b.has(p)))); // {2, 3}
}

源码 https://github.com/webabcd/TypeScriptDemo
作者 webabcd

标签:TypeScript,console,log,mySet,兼收并蓄,set,new,Set
From: https://www.cnblogs.com/webabcd/p/18422246/typescript_basic_set

相关文章

  • 兼收并蓄 TypeScript - 基础: map
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:map示例如下:basic\map.ts{//map是一个key/value集合,先进先出,遇到重复键值则后面的会覆盖前面的,key和value都支持类型的多样性//常规操作有set(),get(),dele......
  • 兼收并蓄 TypeScript - 基础: tuple
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:tuple示例如下:basic\tuple.ts{//tuple-元组leta:[string,number]=['webabcd',22];a[0]='wanglei';a[1]=44;console.log(a,a[0......
  • 兼收并蓄 TypeScript - 类: enum
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-类:enum示例如下:class\enum.ts{//简单枚举enumStatus{ok,error};console.log(Status["ok"],Status["error"]);//01console.log(Status[0],Status......
  • 兼收并蓄 TypeScript - 类: function
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-类:function示例如下:class\function.ts{//定义函数时要指定参数的类型和返回值的类型,无返回值时可以用void表示functionf1(x:number,y:number):number{retur......
  • 兼收并蓄 TypeScript - 类: interface
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-类:interface示例如下:class\interface.ts{//接口用于定义对象的形状,这个是TypeScript的功能(JavaScript中没有)interfacePerson{readonlyid:number;//只读......
  • 兼收并蓄 TypeScript - 基础: 基础
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:基础示例如下:basic\basic.ts{//基础//try/catch/finally的用法functionf1(str:string):number|null{try{letnum=Number(str......
  • 兼收并蓄 TypeScript - 基础: var, let, const
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:var,let,const示例如下:basic\var_let_const.ts//var声明的变量是全局作用域,块外也可用{vara=10;}console.log(a);//let声明的变量是块作用域,仅块内可用{......
  • 兼收并蓄 TypeScript - 基础: 数据类型
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:数据类型示例如下:basic\dataType.ts{//基本数据类型boolean,number,string,symbolleta:boolean=true;letb:number=10;letc:string="abc";......
  • 兼收并蓄 TypeScript - 基础: null, undefined
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:null,undefined示例如下:basic\null_undefined.ts{console.log(undefined==null,undefined===null);//truefalseconsole.log(typeofnull,typeofundefined);......
  • 兼收并蓄 TypeScript - 基础: boolean
    源码https://github.com/webabcd/TypeScriptDemo作者webabcd兼收并蓄TypeScript-基础:boolean示例如下:basic\boolean.ts{leta=true;console.log(a);//true//将指定类型的数据转换为boolean类型console.log(Boolean(100),Boolean(......