'aaasdofjaopfjopaiiisjssfopiasdfffff'.match(/(.)\1+/g) -----> ["aaa", "iii", "ss", "fffff"]
目前分類:JavaScript (33)
- Nov 26 Tue 2024 11:28
[regex]
- Nov 19 Tue 2024 16:45
[Javascript][Typescript] Flat Object
let entry: object = {
a: {
b: {
c: {
dd: 'abcdd',
},
},
d: {
xx: 'adxx',
},
e: 'ae',
},
};
let output: object = {
'a.b.c.dd': 'abcdd',
'a.d.xx': 'adxx',
'a.e': 'ae',
};
private resolveFlatObject(obj: object, parentKey: string = '', result: object = {}): object {
for (const key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
let keyName: string = `${parentKey}${key}`;
if (typeof obj[key] === 'object') {
this.resolveObjctToString(obj[key], `${keyName}.`, result);
} else {
result[keyName] = obj[key];
}
}
}
return result;
}
- Nov 29 Tue 2022 16:56
[Javascript][Typescript] multiple filter 多條件過濾、篩選
let data: Model.IFilterData[] = JSON.parse(JSON.stringify(this.originalFilterData));
let filterKeys: { key: string, value: string }[] = {
{ key: 'name', value: "Name" },
{ key: 'age', value: "Age" },
{ key: 'id', value: "Id" },
{ key: 'email', value: "Email" },
};
- Feb 15 Tue 2022 17:01
[Javascript][Typescript] Convert js / ts string dot notation object to nested object / 將 js / ts 點符號字串物件轉換為巢狀物件
// way 1
setTimeout(() => {
let convertData = resolveDataString(DataString);
console.log(`convertData => `, convertData);
}, 0);
- Feb 15 Tue 2022 16:40
[Javascript][Typescript] Convert js / ts dot notation object to nested object / 將 js / ts 點符號物件轉換為巢狀物件
setTimeout(() => {
let convertData = resolveData(Data);
console.log(`convertData => `, convertData);
}, 0);
- May 05 Wed 2021 23:27
[Javascript][Typescript] nested object recursive / 巢狀 物件 遞迴 反轉物件的 key value
/**
* Please use TypeScript/JS to answer question
* Welcome to answer with unit testing code if you can
*
* After you finish coding, please push to your GitHub account and share the link with us.
*/
// Please write a function to reverse the following nested input value into output value
// Input:
const inputValue: Object = {
aa: {
bb: {
cc: {
dd: {
ee: {
ff: {
gg: {
hh: 'final',
},
},
},
},
},
},
},
};
// Output:
const outputValue: Object = {
final: {
hh: {
ff: {
ee: {
dd: {
cc: {
bb: {
aa: 'final',
},
},
},
},
},
},
},
};
- Feb 19 Fri 2021 21:38
[JavaScript][HTML] textarea auto height 高度隨內容增加
private formTextareaAuto(): void {
var textarea = document.getElementsByTagName('textarea');
setTimeout(() => {
for (var i = 0; i < textarea.length; i++) {
textarea[i].style.height = 'auto';
textarea[i].scrollTop = 0; //防抖動
//有可能這個初始化高度賦值沒有賦上去
textarea[i].style.height = textarea[i].scrollHeight + 'px';
textarea[i].addEventListener('input', (e: any) => {
e.target.style.height = 'auto';
e.target.scrollTop = 0; //防抖動
e.target.style.height = e.target.scrollHeight + 'px';
});
}
}, 200);
}
- Apr 27 Mon 2020 22:57
[JavaScript] [Vue] 套件 vue2-datepicker封裝應用 做出 date range + 快速選擇 元件
<template>
<div class="animated fadeIn">
<div class="flex-right">
<date-picker
:format="datetime.FormatDate"
:title-format="datetime.FormatDate"
:placeholder="_('w_DateSelectDates')"
:range="true"
:clearable="false"
type="date"
class="col-md-3"
v-model="inputDatum.dateRange"
@input="updateDateRange"
>
<template v-slot:sidebar="{ emit }">
<div style="text-align: left">
<button class="mx-btn mx-btn-text col-md-12" @click="selectToday(emit)">Today</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectYesterday(emit)">Yesterday</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectLast7days(emit)">Last 7 days</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectThisWeek(emit)">This Week</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectLastWeek(emit)">Last Week'</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectThisMonth(emit)">This Month</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectLastMonth(emit)">Last Month</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectQ1(emit)">Q1</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectQ2(emit)">Q2</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectQ3(emit)">Q3</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectQ4(emit)">Q4</button>
<button class="mx-btn mx-btn-text col-md-12" @click="selectThisYear(emit)">This Year</button>
</div>
</template>
</date-picker>
</div>
</div>
</template>
- Mar 23 Mon 2020 23:20
[JavaScript] [Vue] 使用 html2Canvas + JsPDF 下載 pdf
<template>
<div>
<b-col cols="6">
<b-button
variant="primary"
@click="pdfExport"
>
<i class="fa fa-file-pdf-o"></i>
Export PDF
</b-button>
</b-col>
</div>
</template>
- Jan 10 Fri 2020 00:21
[JavaScript] [Vue] 套件 vue-ctk-date-time-picker 封裝應用 做出 date range + 快速選擇 元件
<template>
<div>
<p class="title-date" v-if="isTitle">Select Dates</p>
<vue-ctk-date-time-picker
:no-label="true"
:range="true"
:custom-shortcuts="shortcuts"
formatted="YYYY/MM/DD"
format="YYYy/MM/DD"
disabled-dates="disabledDates"
v-model="date"
@is-hidden="validateDate"
@validate="validateDate"
id="range"
>
</vue-ctk-date-time-picker>
</div>
</template>
- Aug 15 Thu 2019 20:26
[JavaScript] 記一次資料整理
interface IEnteredExitedSubtotal { Hour: number; Today: number; Total: number; TotalInTimeSection: number; } interface IInsideSubtotal { Total: number; } interface CameraData { Channel: number; EnteredSubtotal: IEnteredExitedSubtotal; ExitedSubtotal: IEnteredExitedSubtotal; InsideSubtotal: IInsideSubtotal; RuleName: string; UTC: number; } let data = `summary.Channel=123 summary.EnteredSubtotal.Hour=22 summary.EnteredSubtotal.Today=60 summary.EnteredSubtotal.Total=1769 summary.EnteredSubtotal.TotalInTimeSection=60 summary.ExitedSubtotal.Hour=19 summary.ExitedSubtotal.Today=52 summary.ExitedSubtotal.Total=1705 summary.ExitedSubtotal.TotalInTimeSection=52 summary.InsideSubtotal.Total=324 summary.RuleName=NumberStat summary.UTC=1565870249`;
- Jul 02 Tue 2019 21:35
[JavaScript] 日期格式轉換,今天、昨天、本週、上週、本月、上月、本年、日期檢查...
// 注意:回傳值都為string格式,如果要日期格式,
請使用 new Date(this.getDateCount);
// 格式化日期
formatDate(dateArg: any): string {
const date = new Date(dateArg);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const formatMonth = month < 10 ? `0${month}` : month;
const formatDay = day < 10 ? `0${day}` : day;
return `${year}-${formatMonth}-${formatDay}`
// set Date to 00:00:00
DateToZero(value: Date): Date {
let date = new Date(value.getTime());
date.setHours(0, 0, 0, 0);
return date;
}
- May 31 Fri 2019 21:26
[JavaScript] 跑迴圈完成時間選單陣列,小時補am、pm,分鐘補0
// 小時 for (let i = 0; i < 25; i++) { const tempHour = i === 24 ? "00" : i < 10 ? "0" + i.toString() : i.toString(); const tempValue = tempHour + ":00" + (i < 12 || i > 23 ? " am" : " pm"); const tempObject = { value: i.toString(), text: tempValue }; this.dayRanges.hours.push(tempObject); } // 分鐘 for (let i = 0; i < 60; i++) { const tempMinute = i === 60 ? "00" : i < 10 ? "0" + i.toString() : i.toString(); const tempObject = { value: i.toString(), text: tempMinute }; this.dayRanges.minutes.push(tempObject); }
- Apr 15 Mon 2019 23:09
[JavaScript] [Vue] 連動式下拉選單 (不使用套件)
- Apr 04 Thu 2019 16:50
[JavaScript] 正則表達式 IP Port
驗證網址:https://www.regextester.com/22 const ipRegex = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.) {3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/; const portRegex = /^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}| 65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/;
- Mar 27 Wed 2019 22:41
[JavaScript] [vue] 正則表達式 信用卡格式輸入 限制
<form-string :label="_('w_License_License_Key')" v-model="inputLicenseData.licenseKey" :placeholder="_('w_License_License_KeyPlaceholder')" :maxlength="29" ></form-string> // ts寫法 // 限輸入英文、數字,格式:xxxxx-xxxxx-xxxxx-xxxxx-xxxxx @Watch("inputLicenseData.licenseKey") valueChange(value) { this.$nextTick(() => { this.inputLicenseData.licenseKey = value .replace(/\s/g, "") .replace(/[^0-9a-zA-Z]/g, "") .replace(/([0-9a-zA-Z]{5})(?=[0-9a-zA-Z])/g, "$&-"); }); }
- Mar 21 Thu 2019 20:02
[JavaScript] 計算兩個座標點之間的角度
getTwoPointAngle(px1: number, py1: number, px2: number, py2: number) { const x = Math.abs(px1 - px2); const y = Math.abs(py1 - py2); const z = Math.sqrt(x * x + y * y); const angle = Math.round((Math.asin(y / z) / Math.PI) * 180); //最終角度 return angle; }
- Mar 21 Thu 2019 19:58
[JavaScript] 根據經緯度計算與正北方向的夾角,以順時針為方向的角度
- Mar 07 Thu 2019 00:56
[JavaScript] 閉包 作用 / 生命週期
1. 使用函數內部的變數在函數執行完後,仍然存活在記憶體中(延長了區域變數的生命周期) 2. 讓函數外部可以操作(讀 / 寫)到函數內部的數據(變數 / 函數) 問題: 1. 函數執行完後,函數內部聲明的區域變數是否還存在? -- 一般是不存在的,但如果是存在於閉包中的變數才可能存在 2. 在函數外部能直接訪問函數內部的區域變數嗎? -- 不能,但可以通過閉包讓外部操作它
- Mar 06 Wed 2019 23:59
[JavaScript] 閉包 理解 / 常見的閉包
1. 如何產生閉包? -- 當一個嵌套的內部(子)函數引用了嵌套的外部(父)函數的變數(函數)時,就產生了閉包 2. 閉包到底是什麼? -- 使用chrome調試查看 -- 理解一: 閉包是嵌套的內部函數(絕大部分人) -- 理解二: 包含被引用變數(函數)的對像(極少數人) -- 注意: 閉包存在於嵌套的內部函數中 3. 產生閉包的條件? -- 函數嵌套 -- 內部函數引用了外部函數的數據(變數 / 函數) -- 執行外部函數