1

I have an example with some values that I want to change, here the link http://jsfiddle.net/k83hj1jb/

The result is a flat array:

[
    {"name":"options[11][140]","value":"140"},
    {"name":"options[8][99]","value":"99"},
    {"name":"options[8][103]","value":"103"}
]

I want change for something like this:

[
    {
        "options":
        {
            "11":{"140":"140"},
            "8":
            {
                "99":"99",
                "103":"103"
            }
        }
    }
]

Is there a easy way ?

Rolige
  • 993
  • 8
  • 10
  • 1
    They're JavaScript arrays, not jQuery arrays. (In this case, one array with nested non-array objects.) – T.J. Crowder Aug 07 '14 at 19:14
  • There's no particular shortcut, you'll have to parse the `options[11][140]` string, unless you want to use `eval`, which everyone calls "evil" but which is fine **provided** you're in control of the source data. – T.J. Crowder Aug 07 '14 at 19:15
  • Use JSON.parse() to parse the string. Forget `eval()`. – Siddharth Aug 07 '14 at 22:06

2 Answers2

2

Let's call the array you currently have flatArr and the one you want nestArr.

Here's how you could compute nestArr from flatArr:

var getNestArr = function (flatArr) {
    var nestObj = {'options': {}}, obj, opts, parts;
    for (i = 0; i < flatArr.length; i += 1) {
        obj = flatArr[i];
        opts = obj.name;
        parts = opts.replace('options[', '').slice(0, -1).split('][');
        if (!nestObj.options.hasOwnProperty(parts[0])) {
            nestObj.options[parts[0]] = {};
        }
        nestObj.options[parts[0]][parts[1]] = obj.value;
    }
    return [nestObj]; // returns an array instead of object.
};

Testing:

var flatArr = [
    {"name":"options[11][140]","value":"140"},
    {"name":"options[8][99]","value":"99"},
    {"name":"options[8][103]","value":"103"}
];
var nestArr = getNestArr(flatArr);
console.log(JSON.stringify(nestArr));

The output is:

[{"options":{"8":{"99":"99","103":"103"},"11":{"140":"140"}}}] 

The result is exactly what you want. Although, you may want to return nestObj over [nestObj].

Hope this helps.

Sumukh Barve
  • 1,414
  • 15
  • 12
-1

yes, you could use php instead of javascript:

// this is the start json
$json = '[{"name":"options[11][140]","value":"140"},
          {"name":"options[8][99]","value":"99"},
          {"name":"options[8][103]","value":"103"}]';

// decode the json to a php array
$array = json_decode($json, true);

// foreach value of the array
foreach ($array as $value) {

    // initialize a new array
    $option_value = array();

    // access to name value
    $option = $value["name"];

    // explode the string to an array where separator is the square bracket
    $option_values = explode("[", $option);

    // retrieve the two values between the options brackets removing the last square bracket
    $option_value[] = rtrim($option_values[1], "]");
    $option_value[] = rtrim($option_values[2], "]");

    // add a new array element in position of the first value of the option brackets: the array is composed by the second value and the value within the key "value"
    $new_array["options"][$option_value[0]][] = array($option_value[1], $value["value"]);

}

// at the end, encode it to the original json data
$json = json_encode($new_array);

// let show us the result on the screen
var_dump($json);

for any doubt or if i appear you not so clear, tell me and i'm going to edit my answer

blurstream
  • 429
  • 3
  • 13
  • Thanks you @blurstream, but I knows the way to do this on php, the thing is that I need do it in JavaScript, I know it's not easy, do you think can help me do that in JavaScript? – Rolige Aug 07 '14 at 20:35