You could split each string at :\s+ to get a key and the string to be parsed. You'll get 2 values in the resulting array. Use destructuring to get them to separate variables
["CREATE", "id=14&amount=800¤cy=USD"]
Then use URLSearchParams constructor on the second value get a key-value pair from the query string.
Some keys need amount while some keys need id's value. So, you can create a mapper object. This maps the key with the name to be searched inside URLSearchParams.
const mapper = {
PAY: "id",
CREATE: 'amount',
FINALIZE: 'amount',
default: "amount"
};
Here, PAY needs id value. If you have more keys, you could add it here. You could add CREATE: "amount" to this object as well. But, since that is the default, I have skipped that. The default key can be used if a key is not found in mapper. Return an object with the key and the value from the parse function.
Then reduce the array and merge each parsed object to create the output
const actions = ['CREATE: id=14&amount=800¤cy=USD', 'FINALIZE: id=14&amount=800¤cy=USD', 'PAY: id=14']
const mapper = {
PAY: "id",
//CREATE: 'amount', // you could add CREATE and FINALIZE if you want
default: "amount"
};
function parse(str) {
const [key, value] = str.split(/:\s+/);
const param = new URLSearchParams(value).get(mapper[key] || mapper.default);
return { [key.toLowerCase()]: param };
}
const output = actions.reduce((acc, str) => Object.assign(acc, parse(str)), {});
console.log(output)