【发布时间】:2018-10-05 21:33:15
【问题描述】:
我正在创建一个网站,该网站将显示某个地区的所有公共厕所(用于学校项目)。我正在导入一个 JSON 文件,对其进行解析并将其命名为“toiletData”。我想做的是让一个对象负责定义用户搜索的标准。当用户按下“搜索”,并使用选择的条件创建对象时,我试图循环遍历厕所数据以删除任何不适合的条目。问题是该函数没有返回正确的数组,当搜索特定条件时,该函数只返回一些匹配项,即使还有更多匹配项。
搜索对象代码(翻译成英文):
function SearchCriteria() {
this.name = void 0;
this.address = void 0;
this.wheelchairAccesible = void 0;
this.price = void 0;
this.equals = function() {
let matches = Object.values(toiletData.entries);
Object.values(toiletData.entries).forEach(toilet => {
Object.keys(this).forEach(key => {
if ((this[key] !== undefined) && (typeof this[key] !== "function")) {
if (this[key] !== toilet[key]) {
matches.remove(toilet);
}
}
});
});
return matches;
};
}
在 JSON 文件中,与变量关联的值是:
name = string,
address = string,
wheelchairAccesible = 1(yes) or 0(no),
price = int
【问题讨论】:
-
toiletData.entries是一个对象数组还是只是一个对象还是什么? -
toiletData.entries 是一个对象数组。将其打印到控制台返回:[{...}, {...},(更多){...}]
-
我用需要的 JS 代码创建了一个小型 github 存储库github.com/haardes/stackArrayProblem
-
问题中最好包含数据和预期结果。
-
您希望搜索使用
or还是and?您希望搜索包含与每个搜索条件属性匹配的厕所,还是至少包含一个属性?
标签: javascript arrays function object this