-2

I have the string in javascript in the following format,

var a = "";var b ="";var c= "";
var str = "ItemID=123&Name=AO Document&Dept=Testvalue";

I need to split from str and get Itemid value in a,name value in b,Dept value c.How can I do this in javascript ?

Aravi2706
  • 1
  • 2
  • what should a,b,c must contain as a result? – Hameed Syed Jun 15 '17 at 11:21
  • 2
    [How can I get query string values in JavaScript?](https://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript) ? – Alex K. Jun 15 '17 at 11:21
  • Take your time to search before ask. ANyway this is the code you want. `var a = "";var b ="";var c= ""; var str = "ItemID=123&Name=AO Document&Value=Testvalue"; arr=str.split("&"); a=arr[0]; b=arr[1]; c=arr[2]; console.log(a,b,c);` – Sagar V Jun 15 '17 at 11:22

1 Answers1

0

var a = "";var b ="";var c= "";
var str = "ItemID=123&Name=AO Document&Value=Testvalue";

// split the string at the '&' and '=' characters, but use only those parts with odd indices
var parts = str.split(/[&=]/).filter((_, i) => i % 2 === 1);

// assign the values to a, b and c using destructuring
[a, b, c] = parts;

console.log(a, b, c);
PeterMader
  • 6,987
  • 1
  • 21
  • 31