Search code examples
javascriptjqueryobject-literal

nested literal objects and filtering by lower case


I have a page where I receive query string values like the following:

?Target=Customers&Sector=Private

it might also take the same values as small letters, depending on the user.

I have the following object on my page:

var settings = {
    Target = { T1: "Customer", T2: "Partner", T3 : "Other"  }, 
    Sector = { S1 : "Private", S2 : "Public", S3 : "Other" } 
}

I need to take the query string, and get the property names such as T1 , T2 , S1 etc.. as I want to send them somewhere else.

My code to do so looks like this:

//Here I am converting all the main settings properties to small letters, in csae the user passes "customer"
 var settingskey, keys = Object.keys(settings);
    var n = keys.length; 
    var newSettings = {};
    while(n--){
        settingskey = keys[n];
        newSettings[settingskey.toLowerCase()] = settings[settingskey];
    }

//Now I have my new object
    var resultArray = "";  // This is the final array where I will fill the values like T1 S1
    //get query string
    var url = window.location.search;
    //get rid of ? character
    url = url.substring(1);
    //separate each key, value pairs
    var keyvalueArray = url.split('&');
    //loop through all key value pairs
    keyvalueArray.forEach(function(keyvalue){
        //get key, ex: Target
        var key = keyvalue.split('=')[0];
        //get value, ex: Customer
        var val = keyvalue.split('=')[1];
        console.log(key);
        console.log(val);

        for(p in newSettings){
            if(p == key.toLowerCase())
            {
                var element = {};
                element =  newSettings[key.toLowerCase()];   
                var elementValue, elementValues = Object.values(element);
                var newElement = {};
                var n = elementValues.length;
                newElement.keys = element.keys; 
                while(n--){

                    elementValue = elementValues[n];
                    newElement[n] = elementValue.toLowerCase();
                }     
                for(var prop in newElement){
                    if(element[prop] == val){
                        classArray+= "." + prop + ", ";
                    }
                }


          break;
        }
    }
});

resultArray = resultArray.substring(0, resultArray.lastIndexOf(',')).trim();

Basically, am getting the property names the first time, like Target and sector, and converting them to small letters, and creating a new object. Then am "trying" to create a new object for each one with new "value" so instead of Customer I want customer.

Is there an easy and efficient way to compare query string with nested literal object values/keys in small letters? what if I have other 3-4 nested levels, that means more nested loops.

I want when I pass ?target=customer&sector=private

to return: "T1 S1" as a string

If I pass: ?Target=cuSTomer&sector=oThEr

to return: "T1 S3" as a string

Any idea?


Solution

  • You could swap the key/values at the deepest level of your settings, so that you can directly find 'T1' when given 'target' and 'customer'. So the swapped settings object would then look like:

    var settings = {
        target: { customer: "T1", partner: "T2", other: "T3"  }, 
        sector: { private: "S1", public: "S2", other: "S3" } 
    }
    

    Here is a function that makes this swap, and then returns an array of translations made, which you can turn into the space-separated string:

    function getCodes(url, settings) {
        var swapped = Object.keys(settings).reduce( function (obj, key) {
            obj[key.toLowerCase()] = Object.keys(settings[key]).reduce ( function (obj2, key2) {
                obj2[settings[key][key2].toLowerCase()] = key2;
                return obj2;
             }, {} ); 
             return obj;
        }, {} );
        return url.toLowerCase().split(/[?&#]/).slice(1).map( function (arg) {
            var parts = arg.split('=');
            return swapped[parts[0].toLowerCase()][parts[1].toLowerCase()];
        } );
    }
    
    var url= 'http://www.example.com?Target=Customer&Sector=Private';
    
    var settings = {
        Target: { T1: "Customer", T2: "Partner", T3 : "Other"  }, 
        Sector: { S1 : "Private", S2 : "Public", S3 : "Other" } 
    }
    
    var result = getCodes(url, settings).join(' ');
    
    console.log(result);