filtering uom json
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 = uomData.filter(function(element, elIndex) {
return element.Abbrev == "MAS";
})[0];
//now filter the list of uoms based on excludedUoms
var uoms = massUomCategory.Uoms;
console.log("====excluded====");
var excludedUoms = uoms.filter(function(el, idx) {
return (excludedUomIds.indexOf(el.Id) > -1);
});
excludedUoms.forEach(function(el) {
console.log(el.Name);
})
console.log("====included====");
var includedUoms = uoms.filter(function(el, idx) {
return (excludedUomIds.indexOf(el.Id) == -1);
});
includedUoms.forEach(function(el) {
console.log(el.Name);
})