filtering uom json (jquery)
by GregWoods
JavaScript
//cut down list of uom data
var uomData = [
{"Abbrev":"MAS","Title":"Mass","Uoms":[
{"Id":1,"Name":"Kilogram"},
{"Id":2,"Name":"Gram"},
{"Id":3,"Name":"Microgram"},
{"Id":4,"Name":"Milligram"},
{"Id":5,"Name":"Metric Ton"},
{"Id":6,"Name":"Pound"},
{"Id":7,"Name":"Ton (US, Short)"},
{"Id":8,"Name":"Ounce"},
{"Id":232,"Name":"Ton (UK, Long)"}
]},
{"Abbrev":"LEN","Title":"Length","Uoms":[
{"Id":9,"Name":"Kilometer"},
{"Id":10,"Name":"Meter"},
{"Id":18,"Name":"Fathom"}
]}
];
var excludedUomIds = [1,2,4];
//filter will return an filtered list of items. In this case only one item, but it is still an array,
// therefore we need to get the first element of it [0]
var massUomCategory = jQuery.grep(uomData, function(element, elIndex) {
return element.Abbrev == "MAS";
})[0];
//console.log(massUomCategory.Title);
//now filter the list of uoms based on excludedUoms
var uoms = massUomCategory.Uoms;
console.log("====excluded====");
var excludedUoms = jQuery.grep(uoms, function(el) {
return (excludedUomIds.indexOf(el.Id) > -1);
});
jQuery.each(excludedUoms, function(idx, el) {
console.log(el.Name);
})
console.log("====included====");
var includedUoms = jQuery.grep(uoms, function(el) {
return (excludedUomIds.indexOf(el.Id) == -1);
});
jQuery.each(includedUoms, function(idx, el) {
console.log(el.Name);
})