Javascript循环数组并检查逻辑语句

时间:2014-02-28 14:39:58

标签: javascript lodash

我正在尝试对阵列进行一系列逻辑测试。每个数组包含1个ID号和1个项。我需要检查以下内容:如果ID号1出现在一个数组中,它必须包含鞋子或袜子(鞋子或袜子以外的任何东西都返回false)并且它们必须至少出现一次[1,sock]和[1,鞋]。 ID 1不需要发生,后者仅在发生时才适用。同样,ID 2的条件相同。我想给我的函数一个对象数组,告诉它要查找的内容 - 函数中的var f。如果数组中只有一个对象,我的逻辑在这里工作正常,但是一旦我添加更多它就失败了。

我认为我弄错了项目数组。有没有更好的方法来做这样的事情,也许还有减少和/或映射?

var _ = require('lodash');

function d(msg) {
    console.log(msg);
}


var test_data = [[1,'shoe'],[1,'sock'], [2, 'apple'], [2, 'pear']];

function checker(array){
    var f = [{id_num: 1, item: ['shoe', 'sock']}, {id_num: 2, item: ['apple', 'pear']}];

    var mm = true;
    var inLine;

    var valids = function(obj){
        vlist = [];
        _.forEach(obj, function(item){
            vlist.push(item.item);
        });
        return _.flatten(vlist);
    };

    _.forEach(f, function(obj){
        _.forEach(obj.item, function(zone){
            inLine = false;
            _.forEach(t, function(line){
                if(_.contains(valids(f), line[1])){
                    if(_.contains(line, obj.id_num) && _.contains(line, zone)){
                        inLine = true;
                    }
                } else { inLine = false; }
            });
            mm = mm && inLine;
        });
    });
    d(mm);
}

checker(test_data);

预期结果的一个例子是test_data是[[1,'shoe'],[1,'sock']]。预期结果应为True,因为确实发生了2。测试仅在值出现时适用...

1 个答案:

答案 0 :(得分:0)

我的版本:

var f = [{id_num: 1, item: ['shoe', 'sock']}, {id_num: 2, item: ['apple', 'pear']}];

//Reduces our schema to an object using the IDs as keys
var f_new = f.reduce(function(sofar, curr){ sofar[curr.id_num] = curr.item; return sofar},{})


//Helper. Checks if the item is permitted
function valid(item){
      var possible_items = f_new[item[0]+""]
      return _.contains(possible_items, item[1])
}


//Reduces the test data to the same format that f_new
function test_data_occurences (test_data) {
    return test_data.reduce(function(sofar, curr){    
        if(!valid(curr)){throw "Invalid item "+curr}

        var items = sofar[curr[0]]
        if(items===undefined){
            sofar[curr[0]] = [curr[1]]
        }else{
            items.push(curr[1])
        }
        return sofar
    },{})
}

//Does the actual work
function array_valid(array){
    try{
        var array_occurences = test_data_occurences(array)
        //Loops through each ID
            _.each(array_occurences, function(values,id ){
                //Gets the possible values
                var possible_values = f_new[id]
               //Checks if all possible values are contained as actual values
               var contains_all = _.all(possible_values, function(val){return _.contains(values,val)})
               if(!contains_all)throw("Not all values are contained for ID "+id)
            })
        }
    catch(err){
        console.log(error)
        return false
    }
    return true
}


array_valid([[1,'shoe'],[1,'sock'], [2, 'apple'], [2, 'pear']])

array_valid([[1,'shoe'], [2, 'apple'], [2, 'pear']])
//Not all values are contained for ID 1

array_valid([[1,'shoes'],[1,'sock'], [2, 'apple'], [2, 'pear']])
//Invalid item 1,shoes