My working code:
const myString = "a=*aaa;b=*bbb";
let params = [];
myString.split(";").forEach(element => {
let zz = element.split('=');
params.push(zz[1]);
});
console.log(params.map((element, index) => index + '=' + element).join(';'));
params a constIt is an array, and you are just appending items.
You are letting a variable, and then just using it once.
.mapInstead of creating an empty array and appending things to it, you can map the array of ";" separated strings into a corresponding array of the strings you want.
const myString = "a=*aaa;b=*bbb";
const params = myString.split(";").map(
element => element.split('=')[1]
);
console.log(params.map((element, index) => index + '=' + element).join(';'));
Here is a shorter approach, but think about if you want an approach like that. It is not easy to read.
const myString = "a=*aaa;b=*bbb";
const params =
myString
.split(";")
.map((element, index) => {
let zz = element.split('=');
return `${index}=${element.split("=")[1]}`;
})
.join(";");
console.log(params);
I would put them in an object.
const myString = "a=*aaa;b=*bbb";
let params = {};
myString.split(";").forEach(element => {
let zz = element.split('=');
params[zz[0]] = zz[1];
});
console.log(params);