Skip to content

Instantly share code, notes, and snippets.

@dupontdenis
Last active September 20, 2023 06:09
Show Gist options
  • Save dupontdenis/280fa44a5bf2a0d8f5ad167a35950740 to your computer and use it in GitHub Desktop.
Save dupontdenis/280fa44a5bf2a0d8f5ad167a35950740 to your computer and use it in GitHub Desktop.
class BD {
constructor(db) {
this.db = db;
}
/**
* Finds items based on a query given as a JS object
*
* @param {object} query The query to match against (i.e. {name: 'paris'})
* @param {function} callback The callback to fire when the query has
* completed running
**/
find(query, callback) {
callback(this.db.filter(function (city) {
for (const [key, value] of Object.entries(query)) {
if (city[key] !== value) {
return false
}
}
return true
}))
}
}
// cities
const db = new BD([{ name: "Vincennes", population: 40000 },
{ name: "Paris", population: 2161000 }, { name: "roubais", population: 45000 }]);
//console.log(s.db);
db.find({ population: 45000 }, function (cities) {
for (let city of cities) {
city["population"] = 50000;
}
});
db.find({ name: "Paris" }, function (cities) {
for (let city of cities) {
city["capital"] = true;
}
});
console.log(db);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment