Last active
May 3, 2016 22:11
-
-
Save saadshahd/4a212ff2e542ebe8f186cc53f7bd9728 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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