Compare Json
Set the values of the category
JavaScript
var _jsonmeta = {};
//Init category; Need to set each category from Product json
var categories = [undefined,undefined,undefined];
function checkForNullOrZero(categoryidarg, indexarg) {
if (categoryidarg == null || typeof categoryidarg === 'undefined') {
//ignore it if undefined
console.log('Ignore: ' + categoryidarg);
} else if (categoryidarg == 0) {
//check the others because this category is 'None'
console.log('Fall through to next: ' + _jsonmeta[indexarg]);
} else {
//check the hierarchy to see if this Area trumps the others
console.log('Compare: ' + categoryidarg + ' vs ' + _jsonmeta[indexarg]);
if (typeof categories[indexarg] === 'undefined') {
console.log('Returning: ' + categoryidarg);
return categoryidarg; //if undefined, set it.
}
}
console.log('Returning: undefined');
return categories[indexarg]; //undefined
}
function compareJson(json) {
$.each(json, function (i, item) {
//update if applicable
categories[0] = checkForNullOrZero(item.c1, 0);
categories[1] = checkForNullOrZero(item.c2, 1);
categories[2] = checkForNullOrZero(item.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,
...