【问题标题】:Javascript looping over array and checking logical statementsJavascript循环数组并检查逻辑语句
【发布时间】:2014-02-28 14:39:57
【问题描述】:

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

我认为我在展平项目数组时出错了。有没有更好的方法来做这样的事情,也许是减少和或映射?

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 确实发生了。仅当值出现时才适用测试...

【问题讨论】:

    标签: javascript lodash


    【解决方案1】:

    我的版本:

    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
    

    【讨论】:

      猜你喜欢
      • 2012-03-25
      • 1970-01-01
      • 1970-01-01
      • 2021-09-09
      • 2016-02-25
      • 1970-01-01
      • 2016-06-14
      • 2020-12-01
      • 2011-07-19
      相关资源
      最近更新 更多