You could split the parts, collect all items and return a joined string.
var string = '?pn1=age&pn2=name&pv1=12&pv2=alice',
result = string
.slice(1)
.split('&')
.reduce((r, p) => {
var [k, value] = p.split('='),
[key, index] = k.split(/(\d+)/);
index--;
r[index] = r[index] || {};
r[index][key] = value;
return r;
}, [])
.map(({ pn, pv }) => [pn, pv].join(':'))
.join('|');
console.log(result);
URLSearchParams
to convert the query string to a collection of key-value pair. pv
and pn
values based on the number. (\D+)(\d+)
obj.pn
and get the corresponding pv
value for the same number|
This works with pn
and pv
values in any random order
const searchParams = new URLSearchParams("?pn1=age&pn2=name&pv1=12&pv2=alice")
const obj = { pn: {}, pv: {} }
for (let [key, value] of searchParams) {
const [, k, number] = key.match(/(\D+)(\d+)/)
obj[k][number] = value
}
const output = Object.entries(obj.pn)
.map(([n, key]) => `${key}:${obj.pv[n]}`)
.join("|")
console.log(output)
One idea is to first split values on &
and add it to take digit as key and place on object and then later place the respective values in desired format
var str = "?pn1=age&pn2=name&pv1=12&pv2=alice".replace(/^\?/,'')
var strSplit = str.split("&");
let op = strSplit.reduce((op,inp) => {
let [key,value] = inp.split('=')
let digit = key.match(/\d+/)[0]
op[digit] = op[digit] || []
op[digit].push(value)
return op
},{})
let final = Object.values(op).reduce((op,inp) => {
let [key,value] = inp
op.push(`${key}:${value}`)
return op
} ,[]).join(' | ')
console.log(final)
I have a screen where I would like to calculate the next x and y based on angle. The first footstep is this example starts at Step 1. How can I calculate the next footstep where I want to increase ...
I have a screen where I would like to calculate the next x and y based on angle. The first footstep is this example starts at Step 1. How can I calculate the next footstep where I want to increase ...
I have buttons that I make with an each sentence in jade each u in requests form.form-horizontal(method="post") input(type='button', value='Accept #{u.friend}', onclick='doAction(this....
I have buttons that I make with an each sentence in jade each u in requests form.form-horizontal(method="post") input(type='button', value='Accept #{u.friend}', onclick='doAction(this....
It's a simple question. If I click the box , I want to move 200px to the left. but it's hard to me Please help me! where's the problem? <style> .box { width: 200px; height: 200px; ...
It's a simple question. If I click the box , I want to move 200px to the left. but it's hard to me Please help me! where's the problem? <style> .box { width: 200px; height: 200px; ...
I have a function that is triggered when .btn is clicked, it will check if the input and text area with the class .req are filled or not. If not, its suppose to select only the empty elements and add ...
I have a function that is triggered when .btn is clicked, it will check if the input and text area with the class .req are filled or not. If not, its suppose to select only the empty elements and add ...