JavaScript Sort array by custom function in JavaScript sort array custom javascript,sort array custom php,sort array

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);
Answer:1
  • You could use URLSearchParams to convert the query string to a collection of key-value pair.
  • Then loop through them to group the the pv and pn values based on the number.
  • Separate the string and and number values using the regex: (\D+)(\d+)
  • Loop through the obj.pn and get the corresponding pv value for the same number
  • Join the resulting array with |

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)
Answer:2

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)
Answer:3

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....

  1. get value button jquery
  2. get value button javascript
  3. get value button
  4. get value button php
  5. get value button js
  6. get value button onclick
  7. get value button react
  8. get value button click jquery
  9. get value button submit jquery
  10. get value button tkinter
  11. get value button click
  12. get value button android
  13. get value button form
  14. get value button jsp
  15. getting button value html
  16. get button value onclick javascript
  17. get button value onclick react
  18. get button value angular
  19. get button value onclick jquery
  20. get button value using jquery

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; ...

  1. mouse click wont move

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 ...