字符串 转 Object
const strToObj = (str: string)=>{
if(!str) return {};
const objArr = str?.replace(/\{|\}|\s/g,'')?.split(',');
const obj: Record<string,any> = {};
objArr.forEach((its: any)=>{
const oneArr: string[] = its?.split(':');
let val = oneArr[1]?.replace(/'|"/g,'');
if(oneArr.length > 2){
val += `:${oneArr[2]?.replace(/'|"/g,'')}`;
}
obj[oneArr[0]] = val;
});
return obj;
};
字符串 转 Array
const strToArray = (str: string)=>{
if(!str) return [];
const objArr = str?.replace(/\[|\]|\s/g,'')?.split('},');
const allArr: any[] = [];
objArr.forEach((item: any, index: number)=>{
const oneArr = item.replace(/\{|\}/g,'')?.split(',');
oneArr.forEach((its: any)=>{
const twoArr: string[] = its?.split(':');
let val = twoArr[1]?.replace(/'|"/g,'');
if(twoArr.length > 2){
val += `:${twoArr[2]?.replace(/'|"/g,'')}`;
}
allArr[index] ??= {};
allArr[index][twoArr[0]] = val;
});
});
return allArr;
};