On a une BD avec des objets villes. Pour chaque ville, on dispose des propriétés name et population (le nb d'habitants.
db = new BD([{ name: "Vincennes", population: 45000 },
{ name: "Paris", population: 3000000 }, { name: "roubais", population: 45000 }]);
🥇Trouvez toutes les villes avec 45000 habitants pour mettre 50000.
👹Trouvez la ville de Paris pour ajouter la propriétée capitale.
This file contains hidden or 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
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); |