Pass in parameters instead of multiple functions I can't quite figure out the best way to approach this. I have two jQuery objects that both are used to set cookies. I then parse the JSON and set a cookie that is either the users height or the users weight.

This works fine, but I feel like there is an extreme amount of redundant code here that could be cleaned up but for the life of me I'm blanking on how to best do this. Ideally I'd like to be able to somehow condense this and have a function that I can pass in parameters if I want to receive the height cookie or the weight cookie.

Possibly something along the lines of: userInfo(heightCookie) or userInfo(weightCookie).

I'm a bit of a newbie, so code examples are ideal, please.

var height_cookie = {
        name: 'user-height',
        options: {
            path: '/',
            expires: 365
        }
    };
var weight_cookie = {
        name: 'user-weight',
        options: {
        path: '/',
        expires: 365
        }
    };


function userHeightCookie() {
            var userData = $.parseJSON($.cookie(height_cookie.name));
            return(userData);
    };

function userWeightCookie() {
            var userData = $.parseJSON($.cookie(weight_cookie.name));
            return(userData);
    };

function readHeightCookie(userInfo) {
        $.cookie(height_cookie.name, JSON.stringify(userInfo), height_cookie.options);
    };

function readWeightCookie(userInfo) {
        $.cookie(weight_cookie.name, JSON.stringify(userInfo), weight_cookie.options);
    };


var userInfo = readHeightCookie();
var userInfo2 = readWeightCookie();
I'd put the cookies into an object so they can be named.

var userCookies = {
    height: {
        name: 'user-height',
        options: {
            path: '/',
            expires: 365
        }
    },
    weight: {
        name: 'user-weight',
        options: {
            path: '/',
            expires: 365
        }
    }
};


Then you only need read/write cookie methods: (I've not used $.cookie so I'm only guessing I haven't broken it.)

function readCookie(cookieName) {
    var userData = $.parseJSON($.cookie(userCookies[cookieName].name));
    return(userData);
};

function writeCookie(userInfo, cookieName) {
    $.cookie(weight_cookie.name, JSON.stringify(userInfo),userCookies[cookieName].options);
};


If you want to be really modular (like all the cool kids) you might do something like:

var CookieMonster =(function ($) {
    var userCookies = {
        height: {
            name: 'user-height',
            options: {
                path: '/',
                expires: 365
            }
        },
        weight: {
            name: 'user-weight',
            options: {
                path: '/',
                expires: 365
            }
        }
    };
    return {
        readCookie: function readCookie(cookieName) {
            var userData = $.parseJSON($.cookie(userCookies[cookieName].name));
            return(userData);
        },
        writeCookie: function writeCookie(userInfo, cookieName) {
            $.cookie(weight_cookie.name, JSON.stringify(userInfo),userCookies[cookieName].options);
        }
    };
})(jQuery);

var userInfo = CookieMonster.readCookie('height');
var userInfo2 = CookieMonster.readCookie('weight');