0

In mapper object. I have map2 property map2 format is changed compared to others it has inner object. Due to that I put if block in my for loop to map inner object. But I feel its bad thing. If in future I add another property in mapper with nested object means. then I put another else if statement to handle this.. Any other correct way to code?


const mapper = {
    map1: {
        font_size: "font-size",
        font_width: "font-width",
    },
    map2: {
        "line_color": "background-color",
        thickness: {
            "false": "width",
            "true": "height"
        }
    },
    map3: {
        border_radius: "border-radius"
    },
    map4 : {
        border_radius: "border-radius",
        font_width: "font-width"
    },
    map5 : {
        border_radius: "border-radius",
        font_size: "font-size",
    }
}
function styleObjectFetch(mapObject,json) {
    var x = {};
        for (let eachProp of Object.keys(mapObject)) {
            if (json.hasOwnProperty(eachStyleProp)) {
                let value = mapObject[eachProp];
                if (eachProp==="thickness") {
                    value = value[json.vertical];
                }
                    x[value] = json[eachProp];

            }
        }
    return x;
}
function fetchData(mapText,json)
{
    
    if(mapText && mapper[mapText])
    {
        styleObjectFetch(mapper[mapText],json)
    }
}
fetchData("map1",{
    font_size : "30px",
    thickness : "false"
})

I need more flexible and maintaineable code?

kishoreraj
  • 25
  • 4

0 Answers0