Ultimate handling of array-like objects

The string that needs to be processed is var str="[{att=ceshiw,arrid=2343434},{att=ceshiw,arrid=2343434}]"
needs to be processed into var str=[{att:ceshiw,arrid:2343434},{ att: ceshiw, arrid: 2343434}]

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    
    <script>
        var str="[{att=ceshiw,arrid=2343434},{att=ceshiw,arrid=2343434}]"
    var as= str.replace(/\[|\}|\]/g,'')
  var arr= as.split('{')
console.log(arr)
 var arr1=arr.reduce(function(a,b){
    
    
    if(b){
    
    
       var zz= b.split(',').reduce(function(x,y){
    
    
            if(y){
    
    
                var a1=y.split('=')
                x[a1[0]]=a1[1]
            }
            return x
       },{
    
    })
       a.push(zz)
    }
    return a
},[])
console.log(arr1)
    </script>
</body>
</html>

Guess you like

Origin blog.csdn.net/weixin_45176415/article/details/104976258