前言

这篇文章记录学习算法用到的函数,因为经常记不住。(静不下心来导致的)
这下貌似知道怎么分类了,简单记录一下用到过的,下次见到能想起来干啥用的就行。
这 TypeScript 也太好用了,以后就写 TypeScript 了。

tsconfig.json 配置文件

生成中文注释的配置文件

1
tsc --init --locale zh-CN

使用 Vitest 进行测试

下载 Vitest

1
npm install -D vitest

配置 Vitest

创建一个 vitest.config.ts 文件,写入以下内容

1
2
3
4
5
6
7
import { defineConfig } from 'vitest/config'

export default defineConfig({
test: {
include:['./test/**/*.test.ts'],
},
})

安装 VsCode 插件

插件页面寻找 Vitest 插件并下载

Array

与其他编程语言中的数组一样,Array 对象支持在单个变量名下存储多个元素,并具有执行常见数组操作的成员。

Array.prototype.indexOf()

indexOf() 方法返回数组中第一次出现给定元素的下标,如果不存在则返回 -1。

1
2
3
4
5
6
7
8
9
10
11
const beasts = ["ant", "bison", "camel", "duck", "bison"];

console.log(beasts.indexOf("bison"));
//输出结果: 1

//从第二个开始寻找
console.log(beasts.indexOf("bison", 2));
//输出结果: 4

console.log(beasts.indexOf("giraffe"));
//输出结果: -1

Array.prototype.reverse()

reverse() 方法就地反转数组中的元素,并返回同一数组的引用。数组的第一个元素会变成最后一个,数组的最后一个元素变成第一个。换句话说,数组中的元素顺序将被翻转,变为与之前相反的方向。

1
2
3
4
5
6
7
8
9
10
11
const array1 = ["one", "two", "three"];
console.log("array1:", array1);
// 期望输出: "array1:" Array ["one", "two", "three"]

const reversed = array1.reverse();
console.log("reversed:", reversed);
// 期望输出: "reversed:" Array ["three", "two", "one"]

// Careful: reverse is destructive -- it changes the original array.
console.log("array1:", array1);
// 期望输出: "array1:" Array ["three", "two", "one"]

Array.prototype.join()

join() 方法将一个数组(或一个类数组对象)的所有元素连接成一个字符串并返回这个字符串,用逗号或指定的分隔符字符串分隔。如果数组只有一个元素,那么将返回该元素而不使用分隔符。

1
2
3
4
5
6
7
8
9
10
const elements = ["Fire", "Air", "Water"];

console.log(elements.join());
// 期望输出: "Fire,Air,Water"

console.log(elements.join(""));
// 期望输出: "FireAirWater"

console.log(elements.join("-"));
// 期望输出: "Fire-Air-Water"

Array.prototype.push()

push() 方法将指定的元素添加到数组的末尾,并返回新的数组长度。

1
2
3
4
5
6
7
8
9
10
11
const animals = ["pigs", "goats", "sheep"];

const count = animals.push("cows");
console.log(count);
// 期望输出: 4
console.log(animals);
// 期望输出: Array ["pigs", "goats", "sheep", "cows"]

animals.push("chickens", "cats", "dogs");
console.log(animals);
// 期望输出: ["pigs", "goats", "sheep", "cows", "chickens", "cats", "dogs"]

Array.prototype.pop()

pop() 方法从数组中删除最后一个元素,并返回该元素的值。此方法会更改数组的长度。

1
2
3
4
5
6
7
8
9
10
11
12
const plants = ["broccoli", "cauliflower", "cabbage", "kale", "tomato"];

console.log(plants.pop());
// 期望输出: "tomato"

console.log(plants);
// 期望输出: Array ["broccoli", "cauliflower", "cabbage", "kale"]

plants.pop();

console.log(plants);
// 期望输出: Array ["broccoli", "cauliflower", "cabbage"]

Array.prototype.map()

map() 方法创建一个新数组,这个新数组由原数组中的每个元素都调用一次提供的函数后的返回值组成。

1
2
3
4
5
6
7
const array1 = [1, 4, 9, 16];

// 添加函数
const map1 = array1.map((x) => x * 2);

console.log(map1);
// 期望输出: Array [2, 8, 18, 32]

将一个数字转为 Number 类型数组

1
2
3
4
5
6
const num = BigInt("1134454363654634634"); // BigInt() 接受数字、字符串和其他进制数 (例如 16 进制数:"0x1fffffffffffff")
const arr = num.toString().split("").map(Number);

// 或者
const num = BigInt("1134454363654634634"); // BigInt() 接受数字、字符串和其他进制数 (例如 2 进制数:"0b1111111111111111111")
const arr = num.toString().split("").map((x) => Number(x));

Map

Map 对象保存键值对,并且能够记住键的原始插入顺序。任何值(对象或者原始值)都可以作为键或值。

创建一个新 Map

1
2
3
4
5
const myMap = new Map([
[1, "one"],
[2, "two"],
[3, "three"],
]);

Map.prototype.set()

Map 实例的 set() 方法会向 Map 对象添加或更新一个指定的键值对。

1
2
3
4
5
6
7
8
const map1 = new Map();
map1.set("bar", "foo");

console.log(map1.get("bar"));
// 期望输出: "foo"

console.log(map1.get("baz"));
// 期望输出: undefined

Map.prototype.get()

Map 实例的 get() 方法返回该 map 中的指定元素。如果与所提供的键相关联的值是一个对象,那么你将获得该对象的引用,对该对象所做的任何更改都会有效地在 Map 对象中修改它。

1
2
3
4
5
6
7
8
const map1 = new Map();
map1.set("bar", "foo");

console.log(map1.get("bar"));
// 期望输出: "foo"

console.log(map1.get("baz"));
// 期望输出: undefined

Map.prototype.has()

Map 实例的 has() 方法返回一个布尔值,指示具有指定键的元素是否存在。

1
2
3
4
5
6
7
8
const map1 = new Map();
map1.set("bar", "foo");

console.log(map1.has("bar"));
// 期望输出: true

console.log(map1.has("baz"));
// 期望输出: false

Number

Number 值表示像 37 或 -9.25 这样的浮点数。Number 构造函数包含常量和处理数值的方法。其他类型的值可以使用 Number() 函数转换为数字。

Number.prototype.toString()

Number 值的 toString() 方法返回表示该数字值的字符串。

可选参数 radix: 一个整数,范围在 2 到 36 之间,用于指定表示数字值的基数。

1
2
3
4
5
6
7
8
9
10
11
12
function hexColour(c) {
if (c < 256) {
return Math.abs(c).toString(16);
}
return 0;
}

console.log(hexColour(233));
// 期望输出: "e9"

console.log(hexColour("11"));
// 期望输出: "b"

String

String.prototype.split()

split() 方法接受一个模式,通过搜索模式将字符串分割成一个有序的子串列表,将这些子串放入一个数组,并返回该数组。

1
2
3
4
const s = 'III';
const strs = s.split('');
console.log(strs);
//期望输出:['I', 'I', 'I']

String.prototype.slice()

slice() 方法提取字符串的一部分,并将其作为新字符串返回,而不修改原始字符串。

1
2
3
4
5
6
7
8
9
10
11
12
13
const str = "The quick brown fox jumps over the lazy dog.";

console.log(str.slice(31));
// 期望输出: "the lazy dog."

console.log(str.slice(4, 19));
// 期望输出: "quick brown fox"

console.log(str.slice(-4));
// 期望输出: "dog."

console.log(str.slice(-9, -5));
// 期望输出: "lazy"

本篇流水账大概会更新的很慢了…