6

I have a function that is repeated a few times, and I believe it is possible to simplify and send variables from an array.

var i = masterdata.timing.split(',');
        var index = 0;
        for (index = 0; index < i.length; ++index) {
            $("#timing_" + i[index].trim()).prop('checked', true);
        }

    var i = masterdata.concern.split(',');
    var index = 0;
    for (index = 0; index < i.length; ++index) {
        $("#concern_" + i[index].trim()).prop('checked', true);
    }

    var i = masterdata.steps.split(',');
    var index = 0;
    for (index = 0; index < i.length; ++index) {
        $("#steps_" + i[index].trim()).prop('checked', true);
    }

Maybe just change the categories into a variable and send the catergories from an array?

var chkgroup = [
            'timing, concern, steps'
        ]
0

3 Answers 3

4

Your idea should work fine:

var i;
var index = 0;
var j = 0;
var chkgroup = ['timing', 'concern', 'steps'];
var currentGroup;

for (j = 0; j < chkgroup.length; ++j) {
    currentGroup = chkgroup[j];

    i = masterdata[currentGroup].split(',');

    for (index = 0; index < i.length; ++index) {
        $("#" + currentGroup + "_" + i[index].trim())
            .prop('checked', true);
    }        
}

If the chkgroup array really matches the object keys in masterdata, you could use an outer for..in loop instead:

var i;
var index = 0;
var currentGroup;

for (currentGroup in masterdata) {
    i = masterdata[currentGroup].split(',');

    for (index = 0; index < i.length; ++index) {
        $("#" + currentGroup + "_" + i[index].trim())
            .prop('checked', true);
    }        
}

Note that there's no order defined for for...in, so if you need to guarantee that you're iterating over the object properties in a certain order, it might be better to use the predefined array.

You could also get fancy with $.map:

var values = $.map(masterdata, function (i, currentGroup) {
    return $.map(i.split(','), function (val) {
        return $('#' + currentGroup + '_' + val.trim());
    });
});

$(values).prop('checked', true);
Sign up to request clarification or add additional context in comments.

2 Comments

in var i, masterdata.timing changes with the categories.
@triplethreat77: Updated, please take a look
0
var chkgroup = [ 'timing', 'concern', 'steps' ];

setProps( chkgroup, masterdata );

function setProps( c, m ) {
    $.each(c, function(i, group) {
        var i = m[group]split(',');
        var index = 0;
        for (index = 0; index < i.length; ++index) {
            $("#" + group + "_" + i[index].trim()).prop('checked', true);
        }
    });
}

Comments

0

In fact your code is still a bit noisy and not readable, i.e. developer has to spend at least a few minutes analyzing it in order to understand if logic is correct. With the usage of loDash or Underscore, your code can be simplified and turn into this:

   var selector = _.chain(masterdata)
        //pick only needed items 
        .pick(masterdata, 'timing', 'concern', 'steps')
        .map(function (item, key) {
            //split and transform the string into selector, e.g. #timing_1
            var ids = item.split(',');
            var mapped = _.map(ids, function (id) { return "#" + key + "_" + id.trim(); });
            return mapped.join();
        })
        .value()
        .join();
    $(selector).prop('checked', true);

And working sample on jsfiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.