JS - 収集したペンの質問

リクエストパラメータ抽出をゲット

このような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('='// [1] [B]、[2]、[C]、[] [D、XXX] [E ] もし(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))// {[ '1'、 '2']、B:2 ''、C ''、D 'XXX'、E:未定義}

アレイ次元削減

あなたが使用することができますArray.prototype.flat()ES6を+

1 
2
3
4
5
6
 ARR = [ 12 ]、[ 34 ]

LET newArr arr.flat =()// [1,2 ,. 3 ,. 4]既定の寸法低減層

//手写
newArr2 = アレイ([]、ARR).prototype.concat.apply // [1、2、3、4]

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)) // 49

返回 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) // 180

正则

给定字符串 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) // // 1指代第一个括号的匹配项
}

console.log(containsRepeatingLetter(str)) // true

字符串转驼峰

例如: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)) // borderBottomColor

オリジナル:ビッグボックス  JS -収集したペンの質問


おすすめ

転載: www.cnblogs.com/chinatrump/p/11423698.html