//調試技巧一安裝nodejs 使用node命令執行當前的js
/**
* @author: 吳文周
* @name: 默認名稱
* @description: 判斷數組中是否每一項都符合條件
* @param {array,function}: 默認參數
* @return {Boolean}: 默認類型
* @example: 示例isAccord(array, compare)
*/
// 使用說明
const array = [1, 2, 3, 4];
console.log(
array.every(item => {
return item > 0;
})
);
//業務場景回掉函數
//@param {value}: 循環數組中的每一個子
//@param {index}: 循環數組中的索引(可選)
//@param {arr}: 當前數組(可選)
function compare(value, index, arr) {
return value > 1;
}
// 業務封裝
function isAccord(array, compare) {
console.log(array.every(compare));
return array.every(compare);
}
//調用
isAccord(array, compare);
/**
* @author: 吳文周
* @name: 默認名稱
* @description: 判斷數組中是否有一項符合條件
* @param {array,function}: 默認參數
* @return {Boolean}: 默認類型
* @example: 示例isAccord(array, compare)
*/
// 使用說明
var array = [1, 2, 3, 4];
console.log(
array.some(item => {
return item > 0;
})
);
//業務場景回掉函數
//@param {value}: 循環數組中的每一個子
//@param {index}: 循環數組中的索引(可選)
//@param {arr}: 當前數組(可選)
function compare(value, index, arr) {
return value > 1;
}
// 業務封裝
function isAccord(array, compare) {
console.log(array.some(compare));
return array.some(compare);
}
//調用
isAccord(array, compare);
/**
* @author: 吳文周
* @name: 默認名稱
* @description: 數組去重方方法,
* 數組自己大小與數據結構都會對效率有不一樣影響,同一臺機器不一樣時間狀態都有不一樣的執行結果
* @param {array,function}: 默認參數
* @return {Boolean}: 默認類型
* @example: 示例isAccord(array, compare)
*/
var arry = [];
for (let i = 0; i < 1000000; i++) {
let item = parseInt(Math.random() * 10);
arry.push(item);
}
console.time('set');
// var set = Array.from(new Set([...arry]));
var set = [...new Set([...arry])];
console.timeEnd('set');
console.log(set);
console.time('filter');
var filter = arry.filter((value, index, arr) => arr.indexOf(value) === index);
console.timeEnd('filter');
console.log(filter);
console.time('objArry');
var obj = {};
var objArry = [];
for (let item of arry) {
if (!obj[item]) {
obj[item] = 1;
objArry.push(item);
}
}
console.timeEnd('objArry');
console.log(objArry);
console.time('objArryOther');
var objOther = {};
var objArryOther = [];
var length = arry.length;
for (let i = 0; i < length; i++) {
var item = arry[i];
if (!objOther[item]) {
objOther[item] = 1;
objArryOther.push(item);
}
}
console.timeEnd('objArryOther');
console.log(objArryOther);
/**
* @author: 吳文周
* @name: dateFormat
* @description: 時間格式化方法
* @param {String|Number|Date,String}: 默認參數
* @return {String}: 默認類型
* @example: 示例dateFormat('2019-11-01T10:44:57.000+0000', 'YYYY-mm-dd HH:MM:SS')
*/
function dateFormat(time, fmt) {
let date;
if (typeof time === 'object') {
date = time;
} else {
if (typeof time === 'string' && /^[0-9]+$/.test(time)) {
time = parseInt(time);
}
if (typeof time === 'number' && time.toString().length === 10) {
time = time * 1000;
}
date = new Date(time);
}
let ret;
let opt = {
'Y+': date.getFullYear().toString(), // 年
'm+': (date.getMonth() + 1).toString(), // 月
'd+': date.getDate().toString(), // 日
'H+': date.getHours().toString(), // 時
'M+': date.getMinutes().toString(), // 分
'S+': date.getSeconds().toString() // 秒
// 有其餘格式化字符需求能夠繼續添加,必須轉化成字符串
};
for (let k in opt) {
ret = new RegExp('(' + k + ')').exec(fmt);
if (ret) {
fmt = fmt.replace(
ret[1],
ret[1].length == 1 ? opt[k] : opt[k].padStart(ret[1].length, '0')
);
}
}
return fmt;
}複製代碼