Theodore K. Theodore K. - 4 months ago 20
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.

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),