Last active: 3 years ago
数组去重合并
let arr = [
{ name: 'zs', age: 15, happy: ['唱歌', '玩游戏'] },
{ name: 'ls', age: 15, happy: ['玩游戏'] },
{ name: 'zs', age: 15, happy: ['学习', '划水'] },
];
function test(arr) {
let _obj = {};
let _arr = [];
if (!arr.length) return;
arr.map((item) => {
_obj[item.name]?.happy.length
? (_obj[item.name].happy = [..._obj[item.name].happy, ...item.happy])
: (_obj[item.name] = item);
});
_arr = Object.values(_obj);
return _arr;
}
let arr2 = test(arr);
Last active: 3 years ago
数组去重合并
let arr = [
{ name: 'zs', age: 15, happy: ['唱歌', '玩游戏'] },
{ name: 'ls', age: 15, happy: ['玩游戏'] },
{ name: 'zs', age: 15, happy: ['学习', '划水'] },
];
function test(arr) {
let _obj = {};
let _arr = [];
if (arr.length)
arr.map((item) => {
_obj[item.name]?.happy.length
? (_obj[item.name].happy = _obj[item.name].happy.concat(item.happy))
: (_obj[item.name] = item);
});
_arr = Object.values(_obj);
return _arr;
}
let arr2 = test(arr);
Last active: 3 years ago
Fibonacci with generator
function* fibonacciSquence() {
let x = 0,
y = 1;
for (;;) {
yield y;
[x, y] = [y, x + y];
}
}
const fibonacci = (n: number) => {
const arr = new Uint8Array(n + 1);
let x = 0;
for (const i of fibonacciSquence()) {
arr[x++] = i;
if (n-- <= 0) return arr;
}
};
console.log(fibonacci(10));
Last active: 4 years ago
git config --global user.name "DefectingCat"
git config --global user.email i@defect.ink
git config --global user.signingkey 408045F001DF0A9DDFE528C5E011C31AC42BEB98
git config --global commit.gpgsign true
git config --global tag.forcesignannotated true
Last active: 3 years ago
TypeScript Debounce
// "typescript": "~4.1.5"
type Debounce = {
<T extends unknown[]>(fn: (...arg: T) => void | unknown, ms: number): (
this: unknown,
...arg: T
) => void | unknown;
};
/**
* debounce function wrapper
*
* @param {function} fn debounced function
* @param {number} ms delay millisecond
* @return {function} wrapper
*
*/
export const debounce: Debounce = (fn, ms) => {
let timer: number | null = null;
// 需要为this注解类型
return function (...args) {
Last active: 3 years ago
TypeScript Debounce
type Debounce = {
<T extends unknown[], R>(fn: (...arg: T) => R, ms: number): (
this: unknown,
...arg: T
) => void;
};
/**
* debounce function wrapper
*/
export const debounce: Debounce = (fn, ms) => {
let timer: NodeJS.Timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, ms);
};
};
Last active: 4 years ago
VMware Workstation 16 pro serial key
404: Not Found
Last active: 3 years ago
let obj = {
a: "xfy",
b: {
c: "test",
},
d: 123,
e: [1, 2, 3, 4, 5],
f: {
g: 'array',
h: [1, 2, 3, 4, 5, 6, {
i: '嘤嘤嘤'
}]
}
};
let test = {};
function deepCopy(origin, target) {
target = target || {};
let sw = {
Last active: 3 years ago
Fibonacci
function fibonacci(num) {
let count = 0;
let result = [1, 1];
if (num <= 1) {
return result;
}
let n = num - 2;
for (let i = 0; i < n; i++) {
result.push(result[count] + result[count + 1]);
count++;
}
return result;
// let rs = result.reduce((a, b) => a + b);
// return rs;
}
Last active: 3 years ago
访问器属性与私有化变量
let xfy = (() => {
let name = 'xfy';
let age = 18;
let obj = {};
Object.defineProperty(obj, 'test', {
get: function () {
return name + ' : ' + age;
},
set: function (value) {
name = value;
age++;
}
})
return obj;
})();
Last active: 3 years ago
一种不完善的数组去重方法
Array.prototype.unique = function () {
let x,
y;
let uni = {};
for (x of this) {
uni[x] = 'xfy';
}
let arr = Object.keys(uni);
return arr;
}
Array.prototype.unique = function () {
let temp = {},
arr = [];
let len = this.length;
for (let i = 0; i < len; i++) {
if(!temp[this[i]]) {
temp[this[i]] = 'xfy';
arr.push(this[i]);
}
Last active: 3 years ago
一种不完善的数组去重方法
// 不完善
Array.prototype.unique = function () {
let arr = [],
x,
y;
for (x of this) {
if (y === x) continue;
arr.push(x);
y = x;
}
return arr;
}
let arr = [1, 2, 2, 2, 3, 3, 4, 5, 6, 6, 7, 8, 9, 9, 'a', 'a', 'a', 'b', 'b', 'b', 'c'];
arr.unique();
Last active: 3 years ago
利用Object.toString判断更准确的数据类型
// new
function type(target) {
let temp = {
"[object String]": 'ObjectString',
"[object Number]": 'ObejctNumber',
"[object Array]": 'Array',
"[object RegExp]": 'RegExp',
"[object Date]": 'Date'
}
if (target === null) {
return 'null';
}
if (typeof(target) == 'object') {
let str = Object.prototype.toString.call(target);
return temp[str];
} else {
return typeof(target);
}
}
Last active: 3 years ago
利用Object.toString判断更准确的数据类型
// 字符串 数字(new Number()) boolean 对象 数组 函数 null undefined RegExp Date
function type(target) {
let tos = Object.prototype.toString.call(target);
switch (tos) {
case "[object String]":
return 'String';
case "[object Number]":
if (typeof(target) === "number") {
return 'Number';
} else {
return 'Object Number';
}
case "[object Boolean]":
return 'Boolean';
case "[object Object]":
return 'Object';
case "[object Array]":
return 'Array';
case "[object Function]":
return 'Function';