ehime
ehime

Reputation: 8375

Multiple splits into a JSON object?

Hey guys I'm having a bit of trouble with this one, and I was hoping someone could give me a hand. I am trying to break up strings that follow this format foo.bar::baz into an object that resembles this foo.bar.baz = ''

In PHP I would do this with the following

 $obj = New stdClass();
    $inp = [
        'type1',
        'type2',
        'type3.sub1::blah1',
        'type3.sub2::blah1',
        'type4.sub1::blah2',
        'type4.sub2::blah2',
        'type5.sub1',
    ];


    foreach ($inp AS $v)
    {
        if (strpos($v, '.'))
        {
            $b = explode('.', $v);
            $obj->$b[0] = '';

            if (strpos($b[1], '::') && $c = explode('::', $b[1]))
            {
                $obj->$b[0]->$c[0]->$c[1] = '';

            } else {
                $obj->$b[0]->$b[1] = '';
            }

        } else {
            $obj->$v = '';
        }
    }

    print_r($obj);

    stdClass Object
    (
        [type1] =>
        [type2] =>
        [type3] => stdClass Object
            (
                [sub2] => stdClass Object
                    (
                        [blah1] =>
                    )

            )

        [type4] => stdClass Object
            (
                [sub2] => stdClass Object
                    (
                        [blah2] =>
                    )

            )

        [type5] => stdClass Object
            (
                [sub1] =>
            )

    )

I am currently trying to mimic this in Javascript doing the following, but can't seem to get it to behave

var fieldset = results.fooresult.metadata[0].field;
var namespace = [];

// namespace from meta
for (var k in fieldset) {
    if (fieldset.hasOwnProperty(k)) {
        var string = fieldset[k]['$']['NAME'];
        if (0<string.indexOf('.')) {
            var pairs  = string.split('.');
            if (0<pairs[1].indexOf('::')) {
                var value = pairs[1].split("::");
                namespace[pairs[0]][value[0]] = value[1];
            } else {
                namespace[pairs[0]] = pairs[1];
            }
        } else {
            namespace.push(string);
        }

    }
}

Help would be appreciated

Upvotes: 0

Views: 60

Answers (1)

sabof
sabof

Reputation: 8192

This should work:

var inp = [
    'type1',
    'type2',
    'type3.sub1::blah1',
    'type3.sub2::blah1',
    'type4.sub1::blah2',
    'type4.sub2::blah2',
    'type5.sub1'
];

function makeTree(inp) {
    var result = {};
    var split = inp.map(function (str) {
        return str.split(/\.|::/);
    });

    var walk = function (obj, propList) {
        if (propList.length == 1) {
            obj[propList[0]] = '';
            return;
        }
        var nextObj = obj[propList[0]] = obj[propList[0]] || {};
        propList.shift();
        walk(nextObj, propList);
    };

    split.forEach(walk.bind(null, result));
    return result;
}

var out = makeTree(inp);

Upvotes: 1

Related Questions