Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a group of checkboxes on a page that, when clicked, create an array of values (allVals) that change as boxes are checked/unchecked (string output is also an option). I also have another multidimensional array (recordSet), which I would like to filter (in example below: recordSet[i][5]) based on the checked values (allVals), and then return a new multidimensional array with only the matching indexes and their values. In this case it would return only the first recordSet[0] because "cat" was included. Each time a check box is checked/unchecked, recordSet would be filtered again and the newly created multidimensional array would update.

I have looked into underscore.js, which I am also using on the page, but unsure as to how to construct the filter and loops. Any help is much appreciated to point me in the right direction - open for other suggestions to accomplish.

allVals = ["cat", "dog"] 
recordSet = 0: Array[6]
               0: "somevalue"
               1: "somevalue"
               2: "somevalue"
               3: "somevalue"
               4: "somevalue"
               5: "Cat;Monkey;Elephant;Rooster"
             length: 6

            1: Array[6]
               0: "somevalue"
               1: "somevalue"
               2: "somevalue"
               3: "somevalue"
               4: "somevalue"
               5: "Giraffe;Turtle;Rabbit;Snake"
             length: 6

function updateSubTopics() {
    var allVals = [];

    $('.subCheck').each(function() {
        if  (this.checked){
            allVals.push($(this).val());
        }

    });

$(function() {
    $('.subCheck').click(updateSubTopics);
        updateSubTopics();
});
share|improve this question

1 Answer 1

I think you're looking for something like the following. (This example is a proof-of-concept, there's definitely a more efficient way to compute this.)

var animals = ['cat', 'dog'];

var values = [
    ['chicken;cat;', 'foobar'], 
    ['pig;rat;', 'dog;lizard;']
];

_.flatten(_.map(animals, function (animal) {
    return _.flatten(_.map(values, function (value) {
        return _.flatten(_.reject(value, function (x) {
            return x.indexOf(animal) === -1;
        }));
    }));
}));

// ["chicken;cat;", "dog;lizard;"] 

Fiddle

share|improve this answer
    
This looks good, but I only need recordSet[i][5] queried, whereas this seems to query all values in the array. Also, the original multidimensional structure is not returned. I have no way to reference any of the other values I need within each array (those "somevalues" are important in the next steps. You mention a more efficient solution, and I'd love to hear your suggestions. Thanks! –  McKev Jun 13 '13 at 12:32

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.