Compare Json

Set the values of the category

by TheFiddler

JavaScript

var _jsonmeta = {};

    //Init category; Need to set each category from Product json
    var categories = [undefined,undefined,undefined];

function GetFirstNonNull(json, name, categoryIndex) {
    for (var i = 0; i < json.length; i++) {
        
        if (!(json[i][name] == null || typeof json[i][name] === 'undefined' || json[i][name] == 0)) {
            return json[i][name]
        }
    }
    
    return 0;
}
  
    function compareJson(json) {
        //update if applicable
        categories[0] = GetFirstNonNull(json, "c1", 0);
        
        categories[1] = GetFirstNonNull(json, "c2", 1);
        
        categories[2] = GetFirstNonNull(json, "c3", 2);
    }

    _jsonmeta = [{
        "Product": 1,
        "c1": 1111,
        "c2": 1111,
        "c3": undefined
    }, {
        "Product": 2,
        "c1": 2222,
        "c2": 2222,
        "c3": undefined
    }];

    var categories = [undefined,undefined,undefined];
    compareJson(_jsonmeta)
    console.log('Category Results: ' + categories[0] + '|' + categories[1] + '|' + categories[2]); //c1:1111 c2:1111

    _jsonmeta = [{
        "Product": 1,
        "c1": 0,
        "c2": 0,
        "c3": undefined
    }, {
        "Product": 2,
        "c1": 2222,
        "c2": 2222,
        "c3": undefined
    }, {
        "Product": 3,
        "c1": 3333,
        "c2": 3333,
        "c3": undefined
    }]; //c1:2222 c2:2222

    var categories = [undefined,undefined,undefined];
    compareJson(_jsonmeta)
    console.log('Category Results: ' + categories[0] + '|' + categories[1] + '|' + categories[2]);

    _jsonmeta = [{
        "Product": 1,
        "c1": 0,
        "c2": 0,
        "c3": 0
    }, {
        "Product": 2,
        "c1": 0,
        "c2": 0,
        "c3": 0
    }, {
        "Product": 3,
        "c1": 0,
        "c2": 0,
        "c3": 3123
    }, {
        "Product": 4,
        "c1": 4444,
        "c2": 4123,
        "c3": 4444
    }]; //c1:4444 c2:4123 c3: 3123

    var categories =...