0

I have a string:

var data = 'key1=value1&key2=value2&...';

String has a huge number of keys-values combinations. I need to change all values, by some rule - encodeURIComponent it. I need to do that only relatively to values (and maybe keys), but now for the whole string.

Is there complete algorithm to do that?

Stdugnd4ikbd
  • 242
  • 3
  • 9

1 Answers1

1

You can use this code to create an object out of the data:

var data="key1=value1&key2=value2&key3=value3";
var obj=Object.fromEntries(
    data.split("&").reduce((acc,cv)=>{
    acc.push(cv.split("="));
    return acc;
  },[])
);
console.log(obj);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Then you can access whichever key you want and get it's value, say:

obj["key2"]

You might need to unescape values.

iAmOren
  • 2,760
  • 2
  • 11
  • 23