const values = [1, [2, 3, [4], 5], 6] const keys = '[a, [b, c, [d], e], f]' function parse (keys, values) { const isArrayReg = /^\[(.+)]$/ const keyMatchReg = /(.*?)([\[|,\]])(.*)/ const arrayMatch = keys.match(isArrayReg) if (!arrayMatch) return {} let depth = 0 let index = 0 const deepIndexMap = new Map() const deepValueMap = new Map() const result = {} let content = arrayMatch[1] deepIndexMap.set(depth, index) deepValueMap.set(depth, values) while (content) { const keyMatch = content.match(keyMatchReg) if (!keyMatch) { result[content] = values[index] content = '' continue } const key = keyMatch[1].trim() if (key !== '') { result[key] = values[index] index++ } const splitChar = keyMatch[2] if (splitChar === '[') { values = values[index] deepIndexMap.set(depth, index) depth++ index = 0 deepIndexMap.set(depth, index) deepValueMap.set(depth, values) } if (splitChar === ']') { depth-- index = deepIndexMap.get(depth) + 1 values = deepValueMap.get(depth) deepIndexMap.delete(depth) deepValueMap.delete(depth) } content = keyMatch[3].trim() } return result } const result = parse(keys, values) console.log(result)