Get request parameter extraction
There is such a the URL of: http://item.taobao.com/item.htm?a=1&b=2&c=&d=xxx&e, please write a JS process to extract each GET parameters (parameter name and number of uncertainties) URL in , which is returned in the form of a key-value json structure, such as {a `: '. 1' , B: '2', C: '', D: 'XXX', E: undefined}`.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
let str = 'http://item.taobao.com/item.html?a=1&b=2&c=&d=xxx&e&a=2' function (str ) { let params = {} const paramsStr = str.replace(/.*?/ , '' ) paramsStr.split('&' ).forEach(v => { d = v.split('=' ) if (d[0 ] in params) { Array .isArray(params[d[0 ]]) ? params[d[0 ]].push(d[1 ]) : (params[d[0 ]] = [params[d[0 ]], d[1 ]]) } else { params[d[0 ]] = d[1 ] } }) return params } console .log(test(str))
Array dimensionality reduction
You can use Array.prototype.flat()
es6 +
1 2 3 4 5 6
let arr = [[1 , 2 ], [3 , 4 ]]the let newArr arr.flat = () let newArr2 = Array .prototype.concat.apply([], arr)
js computing
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))
Original: Big Box js - pen questions collected