5

I have a string that looks like this:

'a=aaa,bbb,ccc&b=aaa,bbb,ccc&c=aaa,bbb,ccc'

and I want to change it into an object that looks like this:

{ a: 'aaa, bbb, ccc', b: 'aaa, bbb, ccc', c: 'aaa, bbb, ccc' }

I've tried splitting on the & and then = but that results in:

[ ['a', 'aaa','bbb','ccc'], ['b', 'aaa','bbb','ccc'], ['c', 'aaa','bbb','ccc' ] ]

5 Answers 5

7

You could just use URLSearchParams:

var params = new URLSearchParams('a=aaa,bbb,ccc&b=aaa,bbb,ccc&c=aaa,bbb,ccc');
var obj = Object.fromEntries(params.entries())
console.log(obj);

If your browser doesn't have either of those functions, you can use a polyfill:

https://www.npmjs.com/package/url-search-params-polyfill https://github.com/feross/fromentries

Sign up to request clarification or add additional context in comments.

1 Comment

That looks great but I'm getting ReferenceError: URLSearchParams is not defined when I try to run it in my code which is an Ember fastboot environment.
2

If you want to just use split(), you need to make the object out of the split keys and values. You can do this in a reduce loop (or a forEach()):

s = 'a=aaa,bbb,ccc&b=aaa,bbb,ccc&c=aaa,bbb,ccc'
let pairs = s.split('&')                // each pair is like a=aaa,bbb,ccc
let obj = pairs.reduce((obj,data)=> {
    let [k, v] = data.split('=')        // split each pair into key/value
    obj[k] = v                          // add the key to the object
    return obj
}, {})
console.log(obj)

Comments

1

You could use a package like QS or URLSearchParams Polyfill to support any browsers like IE and Node.js as well.

See example below:

console.log(Qs.parse('a=aaa,bbb,ccc&b=aaa,bbb,ccc&c=aaa,bbb,ccc'))
<script src="https://unpkg.com/[email protected]/dist/qs.js"></script>

2 Comments

I am using QS but I still get ReferenceError: URLSearchParams is not defined Any ideas to get it to work?
Looking at QS's code it does not refer to URLSearchParams so it must be coming from another code. QS does its own parsing.
0

You can just iterate thru the resulting array like this-

var myObj = {};
for (var i=0; i<arr.length; i++) {
    for (var j=0; j<arr[i].length; j++) {
        var newProp = arr[i].shift();     
        myObj[newProp] =  arr[i].join()
    }
}

Didn't test it but I think that would work.

1 Comment

I don't think this will work on a string, will probably have to use split on the string first.
0

You can coerce the string into an array of [key, value] entries by first splitting on the &, then mapping over the split output to split once again by = and then use Object.fromEntries.

kv_str = 'a=aaa,bbb,ccc&b=aaa,bbb,ccc&c=aaa,bbb,ccc'
kv_pairs = kv_str.split('&') 
kv_entries = kv_pairs.map(kv => kv.split('='))
kv_obj = Object.fromEntries(kv_entries)
// kv_obj will be: {a: 'aaa,bbb,ccc', b: 'aaa,bbb,ccc', c: 'aaa,bbb,ccc'}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.