JS数据库字数

时间:2016-06-12 22:39:26

标签: javascript

我目前有一个问题,我不知道该怎么做。也许有人可以帮助我。

即,我构建了一个小数据库。首先,出现一个输入窗口。在这个窗口中,我们可以写一个单词(例如:“informatics”),这将保存在我的函数数据库中,并成为一个ID号。

但是我想要更重要的两件事:当两个对象具有相同的名称时(例如:“信息学”),然后将它们放在一起并计算它们。 当有人进入“足球”13次,然后我有一个分子,然后告诉我。

我希望你理解我的意思。

这是我的代码,我非常感谢您的支持。

function Database () {
  this.database = [];
}
  Database.prototype = {
   insert: function (obj) {
    this.database.push(obj);
  },
  where: function (prop, val) {
    if (!prop) {
     // Es wird keine Property übergeben. gib in diesem Fall
     // den Inhalt der gesamten Datenbank zurück.
      return this.database;
    }
    for (var resArr = [], i = 0; i < this.database.length; i++) {
      if (this.database[i][prop] === val) {
        resArr.push(this.database[i]);
       }
     }
     return resArr;
  },
  update: function (whereProp, whereVal, updateProp, updateVal) {
    for (var i = 0; i < this.database.length; i++) {
       if (this.database[i][whereProp] === whereVal) {
         // Objekt gefunden!
          this.database[i][updateProp] = updateVal;
        }
      }
  },
  delete: function (prop, val) {
    if (!prop) {
      // Lösche alles
      var currentDatabase = this.database;
      this.database = [];
      return currentDatabase;
     }
     for (var i = 0; i < this.database.length; i++) {
        if (this.database[i][prop] === val) {
           // Objekt gefunden!
           return this.database.splice(i, 1);
        }
       }
     }
}

function Database () {
  this.database = [];
  var index = 0;
  this insert = function (obj) {
  obj["id"] = this.index++;
  this.database.push(obj);
 };
}

function where () {
    var eingabe = prompt("Nach was suchst du? Gib ein oder zwei Schlüsselbegriffe ein.", "Wort1, Wort2");
   if (eingabe != false) {
     alert("Danke für deinen Eintrag! Er wurde in die Datenbank eingespeichert!");
     db.insert({"apliki": eingabe});
  }

  else {
    alert("Etwas stimmte nicht mit deiner Eingabe...");
    prompt("Bitte gib ein oder zwei Schlüsselbegrifffe ein und achte auf`s Format", "Wort1, Wort2");

  }
}

1 个答案:

答案 0 :(得分:0)

我在跟踪现有代码时遇到了一些麻烦。但如果你只是想数一下,试一试:

var counts = {};
function getCount(word) {
    return counts[word] || 0;
}
function insert(word) {
    counts[word] = getCount(word) + 1;
}

insert('hello');
insert('goodbye');
insert('hello');

console.log(getCount('hello'));   // output: 2
console.log(getCount('goodbye')); // output: 1
console.log(getCount('new'));     // output: 0
相关问题