Theodore K. Theodore K. - 1 year ago 82
JSON Question

Convert string with index in brackets to JSON array

I have various strings with numbers in brackets like

"[4]Motherboard, [25]RAM"
how can I convert such a string to a JSON array (keeping both ids and values) like this:


I'm tried using
to create the array but I really can't find out how to get the inner data in this case.

Answer Source
function toArray(string) {
    return {
        data: string.split(",").map(function(str) {
            str = str.trim();
            return {
                id: str.substring(1, str.indexOf("]")),
                item: str.substring(str.indexOf("]") + 1),
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download