Pages

Callback

class BD{

    constructor(villes){
        this.villes = villes;
    }
   

/**
* Finds items based on a query given as a JS object
*
* @param {object} query The query to match against (i.e. {ville: 'paris'})
* @param {function} callback   The callback to fire when the query has
* completed running
**/
    find (query, callback) {

        callback(this.villes.filter(function(ville) {       
          for (let key in query) {
            if (query[key] !== ville[key]) {
              return false
            }
          }
          return true
        }))
      }
  }

// création d'une base
  bd = new BD([{ville:"vincennes",h:2},{ville:"paris",h:2},{ville:"roubais",h:1}]);

document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p> 1`)


  bd.find({h:2},function(villes){
      for(let ville of villes){
        ville["h"]=30;
      }
  });
document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p> 2`)

  bd.find({ville:"paris"},function(villes){
    for(let ville of villes){
       ville["capitale"]=true;
    }
});

document.body.insertAdjacentHTML("beforeend",`<session>${JSON.stringify(bd.villes)}</session><p>`)
 


https://jsfiddle.net/dupont/Lgre6msz/2/