首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JS数据库字数统计

问JS数据库字数统计
EN

Stack Overflow用户
提问于 2016-06-13 06:39:26
回答 1查看 55关注 0票数 0

我现在有一个问题,我不知道该怎么办。也许有人能帮帮我。

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

但我想要两个更重要的东西:当两个对象具有相同的名称时(例如:“信息”),然后将它们放在一起并对它们进行计数。当一个人输入“足球”13次时,我就有了一个分子,然后给我看一下。

我希望你能理解我的意思。

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

​

代码语言:javascript
复制
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);
        }
       }
     }
}

​

​

代码语言:javascript
复制
function Database () {
  this.database = [];
  var index = 0;
  this insert = function (obj) {
  obj["id"] = this.index++;
  this.database.push(obj);
 };
}

​

​

代码语言:javascript
复制
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");

  }
}

​

EN

回答 1

Stack Overflow用户

发布于 2016-06-13 06:51:15

我在理解您的现有代码时遇到了一些问题。但是,如果你只是想计算一下,试试这个:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37779770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档