A few days ago, a smiling person at the back end called me over and said, “You can solve this parameter by yourself, it is not easy for me to solve this parameter.” As a newcomer, I resolutely accepted the job and looked at the template. {foo1000_soft:”11″,foo1_sn:”2″,foo2_soft:”3.1″,foo2_sn:”4″} At this point I don’t know what will happen.

Parse () with json.stringfy () creates a JSON object with double quotation marks. I didn’t think it was a problem. Use the array and string API directly to solve the problem, rather stupid, the code is as follows

const getBatteryInfo = (originInfoStr) = > {
    const oriInfo = formatBatteryInfo(originInfoStr);
    let batterySN = ' ';
    let batterySoftVersion = ' ';
    if (oriInfo || oriInfo === ' ') {
      return { batterySN, batterySoftVersion };
    }
    for (let i = 1; i <= 3; i++) {
      batterySN += oriInfo[`foo${i}_sn`]? oriInfo[`foo${i}_sn`] : ' ';
      batterySoftVersion += oriInfo[`foo${i}_soft`]? oriInfo[`foo${i}_soft`]
        : ' ';
    }
    return { batterySN, batterySoftVersion };
  };
 const formatBatteryInfo = (originInfoStr) = > {
    if (originInfoStr || originInfoStr == ' ') {
      return ' ';
    }
    let kvList = originInfoStr.split('{') [1].split('} ') [0].split(', ');
    let res = [];
    kvList.map((item) = > {
      let kv = item.split(':');
      res.push(`"${kv[0]}":${kv[1]}`);
    });
    return JSON.parse(` {${res.join(', ')}} `);
  };
Copy the code

It is to remove the curly braces first, and then divide the value according to the comma (face). Later I remembered that when I did the paper test, some of the written test was passed in an array string, I also did the same, feeling very stupid. HHH, online research, found that it seems that we can use the re to deal with, so the code can be simplified as

const str = Foo1_sn ` {foo1000_soft: "11" : "2", foo2_soft: "3.1", foo2_sn: "4"} `;
const kvList = str.match(/foo\d+_(soft|sn):"([0-9]+|[0-9]+.[0-9]+)"/g);
const oriJSON = {};
kvList.map((item) = > {
  oriJSON[item.match(/foo\d+_(soft|sn)/) [0]] = Number(
    item.match(/ (? < = ") ([0-9] + | [0-9] +. [0-9] +)? (? / = ")) [0]); });console.log(oriJSON);// {foo1000_soft: 11, foo1_sn: 2, foo2_soft: 3.1, foo2_sn: 4}
Copy the code

I searched around for a long time while writing (my regex is too bad), and then discovered that I could do a translation using Eval

const str = ` ({foo1_sn foo1000_soft: "11" : "2", foo2_soft: "3.1", foo2_sn: "4"}) `;
console.log(eval(str));/ / {foo1000_soft: '11', foo1_sn: '2', foo2_soft: '3.1' foo2_sn: '4'}
Copy the code

It’s more elegant to write this way (manual dog head), and THEN I found that the old array strings could be rotated this way too

const str = '[1, 2, 3, 4, 5];
console.log(eval(str)); // [1, 2, 3, 4, 5]
Copy the code

If an eval object is experiencing problems, it is possible that it is not wrapped in curly braces with (), like this ({}).

I’ve seen a blog post about eval being invalid in strict mode, but I’ve also seen a situation where I can’t use regular expressions in strict mode, so I have to use my first twist…..

If there is a better conversion method, welcome to add