リクエストパラメータ抽出をゲット
このようなURLがあります: http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx&eは、各パラメータ(パラメータ名および不確定要素の数)GET URLを抽出するためのJSプロセスを書いてください、などのキーと値のJSON構造の形で返される{ ':」1' 、B '2'、C ''、D 'XXX'、E:未定義} `。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
聞かせて STR = 'http://item.taobao.com/item.html?a=1&b=2&c=&d=xxx&e&a=2' 関数 ( STR ) { せ のparams = {} CONST (paramsStr = str.replace /.*?/ 、 '' ) paramsStr.split( '&' ).forEach( V => { D = v.split( '=' ) もし (D [ 0 ] 内 のparams){ アレイ .isArray(paramsは[D [ 0 ])のparams [D [? 0 ]プッシュ(D [ 1 ]):(paramsは[D [ 0 ] = [ paramsは[D [ 0 ]、D [ 1 ]) } 他 { paramsは[D [ 0 ] = D [ 1 ] } }) 戻り paramsは } コンソール の.log(テスト(STR))
アレイ次元削減
あなたが使用することができます Array.prototype.flat()
ES6を+
1 2 3 4 5 6
せ ARR = [ 1 、 2 ]、[ 3 、 4 ] LET newArr arr.flat =() せ newArr2 = アレイ ([]、ARR).prototype.concat.apply
JS・コンピューティング
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const arr = [1 , 8 , 6 , 2 , 5 , 4 , 8 , 3 , 7 ]function maxArea (arr ) { let [start, area] = [0 , 0 ] let end = arr.length - 1 while (start < end) { const h = Math .min(arr[start], arr[end]) const result = h * (end - start) area = result > area ? result : area arr[start] > arr[end] ? end-- : start++ } return area } console .log(maxArea(arr))
返回 1 到 400 所有自然数中一共出现过多少次“1”,如 1 到 21 一共出现过 13 次“1”
1 2 3 4 5 6 7
let count = 0 for (let num = 1 ; num <= number; num++) { ;`${num} ` .replace(/1/g , () => count++) } console .log(count)
正则
给定字符串 str,检查其是否包含连续重复的字母(a-zA-Z),包含返回 true,否则返回 false
1 2 3 4 5 6
let str = 'adfdsaccsdd' function containsRepeatingLetter (str ) { return /([a-zA-Z])1/ .test(str) } console .log(containsRepeatingLetter(str))
字符串转驼峰
例如:border-bottom-color
—-> borderBottomColor
1 2 3 4 5 6 7 8 9 10 11
let str = 'border-bottom-color' function toHump (params ) { let newStr = '' params.split('-' ).forEach((d, i ) => { newStr += i === 0 ? d : `${d.charAt(0 ).toUpperCase()} ${d.substring(1 )} ` }) return newStr } console .log(toHump(str))
オリジナル: ビッグボックス JS -収集したペンの質問