https: / / designer. mocky. io
Date. prototype. toFormattedString = function ( format) {
const O = {
"M+" : this . getMonth ( ) + 1 ,
"d+" : this . getDate ( ) ,
"H+" : this . getHours ( ) ,
"h+" : this . getHours ( ) ,
"m+" : this . getMinutes ( ) ,
"s+" : this . getSeconds ( ) ,
"q+" : Math. floor ( ( this . getMonth ( ) + 3 ) / 3 ) ,
"f" : this . getMilliseconds ( )
} ;
if ( /(y+)/ . test ( format) ) {
format = format. replace ( RegExp. $1 , ( this . getFullYear ( ) + "" ) . substr ( 4 - RegExp. $1. length) ) ;
}
for ( let k in O ) {
if ( new RegExp ( "(" + k + ")" ) . test ( format) ) {
format = format. replace ( RegExp. $1 , ( RegExp. $1. length === 1 ) ? ( O [ k] ) : ( ( "00" + O [ k] ) . substr ( ( "" + O [ k] ) . length) ) ) ;
}
}
return format;
}
let date = new Date ( ) ;
date. toFormattedString ( 'yyyy-MM-dd HH:mm:ss' ) ;
date. toFormattedString ( 'yy/MM/dd' ) ;
date. toFormattedString ( 'M月d日' ) ;
date. toFormattedString ( 'HH:mm:ss' ) ;
date. toFormattedString ( 'm分,s秒' ) ;
function deepClone ( obj) {
let newObj = Array. isArray ( obj) ? [ ] : { }
if ( obj && typeof obj === "object" ) {
for ( let key in obj) {
if ( obj. hasOwnProperty ( key) ) {
newObj[ key] = ( obj && typeof obj[ key] === 'object' ) ? deepClone ( obj[ key] ) : obj[ key] ;
}
}
}
return newObj
}
let obj = {
school: {
class1: {
student: 50
}
}
}
function safeProps ( func, defaultVal) {
try {
return func ( ) ;
} catch ( e ) {
return defaultVal;
}
}
safeProps ( function ( ) {
student = obj. school. class1. student
} , - 1 )
key: [ 'key0' , 'key1' ] ;
value: [ 'value0' , 'value1' ] ;
[ { key0: 'value0' } , { key1: 'value1' } ]
Object. fromEntries ( key. map ( ( k, i) => [ k, value[ i] ] ) )
'form.input' . split ( '.' ) . reduce ( ( prev, cur) => prev[ cur] , {
form: {
input: 1
}
} )
result: 1
[ ... Array ( 221 ) . keys ( ) ] . slice ( 140 )
function * random ( ) {
let start = 10000
while ( 1 ) {
start += new Date ( ) . getTime ( ) % 1000
yield start
}
}
result: random ( ) . next ( )
const result = '[{"id":1234567890},{"id":1234567891}]' . replace ( /(?<="id"\s*:\s*)(\d+)/g , '"$1"' )
result: [ { "id" : "1234567890" } , { "id" : "1234567891" } ]
let x = a? . e? . f
let x = ( a !== null && a !== undefined) && ( a. e !== null && a. e !== undefined) && a. e. f;
const type = [
{ num: "A" , content: "I'm A." } ,
{ num: "B" , content: "I'm B." } ,
{ num: "C" , content: "I'm C." }
]
const target = { }
type. forEach ( i => target[ "choice" + i. num ] = i. content)
result: console. log ( target)
const treeData = [ {
title: "1" ,
key: "1" ,
children: [ {
title: "1-1" ,
key: "1-1" ,
children: [ {
title: "1-1-1" ,
key: "1-1-1" ,
} , {
title: "1-1-2" ,
key: "1-1-2" ,
} ]
} , {
title: "1-2" ,
key: "1-2" ,
} , {
title: "1-3" ,
key: "1-3" ,
} , {
title: "1-4" ,
key: "1-4" ,
} ] ,
} ] ;
function f ( arr, selectedKey) {
return arr. filter ( item => item. key !== selectedKey) . map ( item => {
item = Object. assign ( { } , item)
if ( item. children) {
item. children = f ( item. children, selectedKey)
}
return item
} )
}
result: f ( treeData, '1-2' )
const data = {
'value' : '110000' ,
'label' : '北京市' ,
'children' : [
{
'value' : '110100' ,
'label' : '北京市' ,
'children' : [
{
'value' : '110101' ,
'label' : '东城区'
}
]
}
]
}
function findCodeByName ( data, nameList) {
if ( nameList. length === 0 ) return [ ] ;
const [ name, ... rest] = nameList;
const item = data. find ( i => i. label === name) ;
if ( ! item) throw new Error ( name + ' cannot be found in the list!' ) ;
return [ item. value, ... findCodeByName ( item. children, rest) ] ;
}
result: findCodeByName ( data, [ '天津市' , '天津市' , '和平区' ] )