我现在有一个问题,我不知道该怎么办。也许有人能帮帮我。
也就是说,我构建了一个小数据库。首先,出现一个输入窗口。在这个窗口中,我们可以写一个单词(例如:"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");
}
}
发布于 2016-06-13 06:51:15
我在理解您的现有代码时遇到了一些问题。但是,如果你只是想计算一下,试试这个:
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: 0https://stackoverflow.com/questions/37779770
复制相似问题