Juvy Cagape Juvy Cagape -4 years ago 114
Javascript Question

Javascript arrays builder with multiple sting keys

Is there any great idea there that can help me solve my problem regarding the javascript multidimensional arrays builder?

The things that I really want to do is that... i.e I have a form field with name '

fields.0.fld.0.first_name
', that field name needs to transform into javascript array format. The 0 here means as identifier, it classify that the 'fields' word in that field name must / expected to be started as '
new Array()
' because that'll handle multiple sub elements without specific index. And some other strings (a non-integer) that'll only be expected as '
Object
' type of js array or simply as '{}' (that's the last part of the field name the '
first_name
', that will be formatted like '
{first_name: value}
').

Now, since the purpose is to get the fields in the form, I'm looking for a function that will auto generate a constructed json format of those fields with value of course :-). The sample json format output must be...

{"fields" : [
{
"_id" : "",
"type" : "text",
"name" : "test1",
"id" : "",
"class" : "",
"conClass" : "s12",
"label" : "",
"required" : false,
"deleted" : false
},
{
"_id" : "",
"type" : "group",
"gStart" : "",
"gEnd" : "",
"flds" : [
{
"_id" : "",
"type" : "text",
"name" : "test2",
"id" : "",
"class" : "",
"conClass" : "s12",
"label" : "",
"required" : false,
"deleted" : false
}
]
}
]
}

Answer Source

Ok, I've solved it. Based on the naming structure and required logic, I've discovered that the js eval is very useful. Now I can create multi-demensional array of js using the function below.

function _arr_definer(items, _arr_handler, _curr, _val)  
    {
        var _tmp_indexes = "";
        if(_curr > 0){
            for(var _i = 0; _i < _curr; _i++){
                if(Number(items[_i]) >= 0)
                    _tmp_indexes += "[" + items[_i] + "]";
                else
                    _tmp_indexes += "['" + items[_i] + "']";
            }

            if(typeof eval("_arr_handler" + _tmp_indexes + "['" + items[_curr] + "']") == 'undefined'){
                if(Number(items[_curr]) >= 0){
                    if(typeof items[_curr + 1] != 'undefined'){
                        if(Number(items[_curr + 1]) >= 0){
                            var _temp_sub = [];
                            if(typeof eval("_arr_handler" + _tmp_indexes) != 'undefined')
                                eval("_temp_sub = _arr_handler" + _tmp_indexes);
                            _temp_sub[items[_curr]] = [];
                            eval("_arr_handler" + _tmp_indexes + " = _temp_sub;");   
                        }else{
                            var _temp_sub = [];
                            if(typeof eval("_arr_handler" + _tmp_indexes) != 'undefined')
                                eval("_temp_sub = _arr_handler" + _tmp_indexes);

                            _temp_sub[items[_curr]] = {};
                            eval("_arr_handler" + _tmp_indexes + " = _temp_sub;");   
                        }
                    }else{ //assign value
                        eval("_arr_handler" + _tmp_indexes + "['"+ items[_curr] +"'] = _val;");
                    }
                }else{
                    if(typeof items[_curr + 1] != 'undefined'){
                        if(Number(items[_curr + 1]) >= 0){
                            var _temp_sub = {};
                            if(typeof eval("_arr_handler" + _tmp_indexes) != 'undefined')
                                eval("_temp_sub = _arr_handler" + _tmp_indexes);
                            _temp_sub[items[_curr]] = [];
                            eval("_arr_handler" + _tmp_indexes + " = _temp_sub;");   
                        }else{
                            var _temp_sub = {};
                            if(typeof eval("_arr_handler" + _tmp_indexes) != 'undefined')
                                eval("_temp_sub = _arr_handler" + _tmp_indexes);
                            _temp_sub[items[_curr]] = {};
                            eval("_arr_handler" + _tmp_indexes + " = _temp_sub;");   
                        }
                    }else{ //assign the value
                        eval("_arr_handler" + _tmp_indexes + "['"+ items[_curr] +"'] = _val;");
                    }
                }
            }
        }else{
            if(typeof eval("_arr_handler" + _tmp_indexes + "['"+ items[_curr] +"']") == 'undefined'){
                if(typeof items[_curr + 1] != 'undefined'){
                    if(Number(items[_curr + 1]) >= 0){
                        eval("_arr_handler" + _tmp_indexes + "['"+ items[_curr] +"'] = [];");
                    }else{
                        eval("_arr_handler" + _tmp_indexes + "['"+ items[_curr] +"'] = {};");
                    }
                }else{ //assign the value
                    eval("_arr_handler" + _tmp_indexes + "['"+ items[_curr] +"'] = _val;");
                }
            }
        }

        if(typeof items[_curr + 1] != 'undefined'){
            _arr_definer(items, _arr_handler, _curr + 1, _val);
        }
    }

And for using it just follow the declaration below.

var _arr_handler = {};

    var _items = "fields.0.flds.0.conClass".split('.');
    _arr_definer(_items, _arr_handler, 0, 'This is a testing content');

    var _items = "fields.0.flds.0.first_name".split('.');
    _arr_definer(_items, _arr_handler, 0);

    var _items = "fields.0.flds.0.subnames.0.nice".split('.');
    _arr_definer(_items, _arr_handler, 0);


    var _items = "other.cool.cool1.cooler.test".split('.');
    _arr_definer(_items, _arr_handler, 0);

    var _items = "other.cool.cool1.othersubs.0".split('.');
    _arr_definer(_items, _arr_handler, 0, true);

    var _items = "other.cool.cool1.othersubs.1".split('.');
    _arr_definer(_items, _arr_handler, 0, false);

    var _items = "more_field_1".split('.');
    _arr_definer(_items, _arr_handler, 0);

    var _items = "more_field_2".split('.');
    _arr_definer(_items, _arr_handler, 0);

    var _items = "more_field_3".split('.');
    _arr_definer(_items, _arr_handler, 0);

    var _items = "more_field_4.subs.0.item_1".split('.');
    _arr_definer(_items, _arr_handler, 0);

    var _items = "more_field_4.subs.0.item_2".split('.');
    _arr_definer(_items, _arr_handler, 0);

    var _items = "more_field_4.subs.0.item_3".split('.');
    _arr_definer(_items, _arr_handler, 0);

    var _items = "more_field_4.subs.0.item_4".split('.');
    _arr_definer(_items, _arr_handler, 0);

    console.log(_arr_handler);

The purpose of this is to... save the MongoDB document in one trigger only including the sub->sub->sub->sub elements. :-)

Cheers

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download