说明:
实现destructuringArray方法,达到如下效果
destructuringArray( [1,[2,4],3], "[a,[b],c]" );
result
{ a:1, b:2, c:3 }
const destructuringArray = (arr, str) => {
const handleAfterStr = Array.isArray(str) ? str : JSON.parse(str.replace(/([a-z]+)/g, '"$1"'));
let obj = {}
for (let i = 0; i < handleAfterStr.length; i++) {
if (Array.isArray(handleAfterStr[i])) {
Object.assign(obj, destructuringArray(arr[i], handleAfterStr[i]));
} else {
obj[handleAfterStr[i]] = arr[i];
}
}
return obj;
};
console.log(destructuringArray([1, [2, 4], 3], "[a,[b],c]"));