Skip to content

Instantly share code, notes, and snippets.

@egalink
Last active May 28, 2020 00:52
Show Gist options
  • Select an option

  • Save egalink/12d02377a4dfe36f620bc36f5312ce1f to your computer and use it in GitHub Desktop.

Select an option

Save egalink/12d02377a4dfe36f620bc36f5312ce1f to your computer and use it in GitHub Desktop.
var collection = [
{item: "lapiz", cost: 10.2, vendor: "mari"},
{item: "lapiz", cost: 11.2, vendor: "maris"},
{item: "lapiz", cost: 8.24, vendor: "maris"},
{item: "lapiz", cost: 8.232, vendor: "maris"},
{item: "lapiz", cost: 8.23198, vendor: "maris"},
{item: "lapiz", cost: 8.23199, vendor: "maris"},
{item: "lapiz", cost: 8.25, vendor: "maris"},
{item: "lapiz", cost: 8.256, vendor: "maris"},
{item: "lapiz", cost: 9, vendor: "mariss"},
{item: "lapiz", cost: 900, vendor: "mariss"},
{item: "lapiz", cost: 9.1, vendor: "mariss"},
{item: "lapiz", cost: 9.02, vendor: "mariss"}
];
function withMinCost (collection) {
//
var dictionary = {};
collection.forEach(function (obj) {
var i = Number(obj.cost);
if (! isNaN(i)) {
dictionary[i] = obj;
}
});
var minCost = Object.keys(dictionary).sort(function (a, b) { return a - b });
return dictionary[minCost[0]];
}
withMinCost(collection);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment