0

I have a following string:

var str = "name=test1^assigned_to=12345678901112131415161718991234^business_service=99945678901112131415161718991211^EQ";
var obj = { name: 'test1',
            assigned_to: '12345678901112131415161718991234',
            business_service: '99945678901112131415161718991211'
          };

and would like to create object of specific keys and values from that string as is in my example, has anyone achieved similar thing? How?

John Long
  • 39
  • 8

1 Answers1

1

let str = "name=test1^assigned_to=12345678901112131415161718991234^business_service=99945678901112131415161718991211^EQ";

let obj = {}
str.split('^').forEach(e => {
  let x = e.split('=');
  if (x[1])
    obj[x[0]] = x[1]
});

console.log(obj);

You can remove if (x[1]) condition;

adiga
  • 28,937
  • 7
  • 45
  • 66
Hrishi
  • 1,082
  • 1
  • 10
  • 25
  • [How do I create a runnable stack snippet?](https://meta.stackoverflow.com/questions/358992) – adiga Jul 28 '20 at 14:21