admin管理员组

文章数量:1351984

How does one convert string array values into a Javascript array object?

var querystring = 'lang[]=EN&lang[]=FR&type[]=1&type[]=2';

Into:

{
    lang: ['EN', 'FR'],
    type: ['2']
}

Below my attempt:

let params = new URLSearchParams(decodeURI(querystring));
let entries = params.entries();

let result = {}
for(let entry of entries) { // each 'entry' is a [key, value]
    result[entry[0]] = entry[1];
}
return result;

Which results in:

[{
    "name": "lang[]",
    "value": "EN"
}, {
    "name": "lang[]",
    "value": "FR"
}, {
    "name": "type[]",
    "value": "2"
}]

How does one convert string array values into a Javascript array object?

var querystring = 'lang[]=EN&lang[]=FR&type[]=1&type[]=2';

Into:

{
    lang: ['EN', 'FR'],
    type: ['2']
}

Below my attempt:

let params = new URLSearchParams(decodeURI(querystring));
let entries = params.entries();

let result = {}
for(let entry of entries) { // each 'entry' is a [key, value]
    result[entry[0]] = entry[1];
}
return result;

Which results in:

[{
    "name": "lang[]",
    "value": "EN"
}, {
    "name": "lang[]",
    "value": "FR"
}, {
    "name": "type[]",
    "value": "2"
}]
Share Improve this question edited Dec 20, 2018 at 2:19 bart asked Dec 20, 2018 at 2:03 bartbart 15.3k22 gold badges83 silver badges109 bronze badges 5
  • don't understand the expected output, type: [1,2] would make a little sense. – xianshenglu Commented Dec 20, 2018 at 2:08
  • @xianshenglu "type" would be an array with IDs in it. – bart Commented Dec 20, 2018 at 2:11
  • Why not use something like npmjs./package/query-string or npmjs./package/qs – Bergur Commented Dec 20, 2018 at 2:15
  • @bart, that's not consistent with lang: [EN, FR] – xianshenglu Commented Dec 20, 2018 at 2:17
  • @xianshenglu Let me update the above example. – bart Commented Dec 20, 2018 at 2:19
Add a ment  | 

3 Answers 3

Reset to default 3

Simplest version would be to use a library like qs

https://www.npmjs./package/qs#parsing-arrays

var withArray = qs.parse('a[]=b&a[]=c');

ressults in

a: ['b', 'c'] 

You should probably test for the [] so your code won't break with entries not ending in []. You can then slice off the [] so your keys don't include it:

var querystring = 'lang[]=EN&lang[]=FR&type[]=1&type[]=2&test=hello';

let params = new URLSearchParams(decodeURI(querystring));
let entries = params.entries();

let result = {}
for(let entry of entries) { // each 'entry' is a [key, value]
    let [key, val] = entry
    if (key.endsWith('[]')){         // array 
      key = key.slice(0,-2);         // clean up the key
      (result[key] || (result[key] = [])).push(val)
    } else {
      result[key] = val             // normal parameter
    }
}
console.log(result)

var querystring = 'lang[]=EN&lang[]=FR&type[]=1&type[]=2';
let params = new URLSearchParams(decodeURI(querystring));
let entries = params.entries();

let result = {}
for(let entry of entries) { // each 'entry' is a [key, value]
    var key = entry[0];
    var val = entry[1];
    if(key in result){
        result[key].push(val);
    }else{
        result[key] = [val];
    }
}
console.log(result);

本文标签: javascriptConvert string array values from URLSearchParamsStack Overflow