Skip to content

Instantly share code, notes, and snippets.

@saadshahd
Last active May 3, 2016 22:11
Show Gist options
  • Save saadshahd/4a212ff2e542ebe8f186cc53f7bd9728 to your computer and use it in GitHub Desktop.
Save saadshahd/4a212ff2e542ebe8f186cc53f7bd9728 to your computer and use it in GitHub Desktop.
// Generates two conditional methods
// 1. (isBroken: true, method: 'some') filters the abandoned shipments
// 2. (isBroken: false, method: 'every') filters the ready shipments
function checkShipment(isBroken, method) {
return function (curShipment) {
return curShipment.packagesList[method](function (curPackage) {
return curPackage.isBroken === isBroken;
});
};
}
// Data
var shipmentList = [{
id: 1,
packagesList: [
{ id: 1, isBroken: true },
{ id: 2, isBroken: false },
{ id: 3, isBroken: true },
{ id: 4, isBroken: false }
]
}, {
id: 2,
packagesList: [
{ id: 1, isBroken: false },
{ id: 2, isBroken: true },
]
}, {
id: 3,
packagesList: [
{ id: 1, isBroken: false },
{ id: 2, isBroken: false },
]
}];
// Filtering
var abandonedShipmentList = shipmentList.filter(checkShipment(true, 'some'));
var readyShipmentList = shipmentList.filter(checkShipment(false, 'every'));
// To LOG results
function getID(shipment) {
return shipment.id;
}
var abandonedIDs = abandonedShipmentList.map(getID);
var readyIDs = readyShipmentList.map(getID);
// [1, 2] [3]
console.log(abandonedIDs, readyIDs);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment