[Solved] How to convert the string to object in JavaScript?


One more Implementation:

//str format "res=[xyz=name,abc=address]"
function stringToObject(str){
  const extractedStr = str.substring(str.indexOf("[")+1,str.indexOf("]")-1);
  return extractedStr.split(",").reduce((acc,keyVal)=>{
    acc[keyVal.split("=")[0]] = keyVal.split("=")[1];
    return acc;
  },{});
}

console.log(stringToObject("res=[xyz=name,abc=address]"));

1

solved How to convert the string to object in JavaScript?