javascript - JS:TypeError:在对象中找不到功能键
问题描述
我在应用程序脚本中有以下内容:
function getQAs() {
return [
{ "Do you have any pictures ?|1 ": {"yes":2,"no":3 } },
{ "Do you have any pictures ?|2 ": {"yes":2,"no":3 } },
{ "Do you have any pictures?|3 ": {"yes":2,"no":3 } },
]
}
我正在尝试构建一个函数,该函数将通过对象的键搜索数字。我正在使用数字 1 进行测试。当我运行时:
function testQA() {
var qa = getQAs();
var matches = qa.keys().filter(function(row) { //ONLY CHECKED ROWS.
Logger.log(row)
return row.indexOf('1') == true;
});
Logger.log(matches);
}
我明白了
JS:TypeError:在 object 中找不到功能键。我究竟做错了什么?
解决方案
您需要使用for...in
循环来获取对象的键。我设计了一个通过键的简单循环来确定键中是否存在值,然后推出一个过滤数组
function testQA() {
var qa = getQAs();
function getRow(row_identifier) {
var filtered = [];
qa.forEach(function(v) {
for(var k in v) {
if(k.indexOf(row_identifier) > 0) filtered.push(v);
}
});
return filtered;
}
return getRow(row_identifier);
}
function getQAs() {
return [
{
"Do you have any pictures ?|1 ": {
"yes": 2,
"no": 3
}
},
{
"Do you have any pictures ?|2 ": {
"yes": 2,
"no": 3
}
},
{
"Do you have any pictures?|3 ": {
"yes": 2,
"no": 3
}
},
]
}
function testQA() {
var qa = getQAs();
function getRow(row_identifier) {
var filtered = [];
qa.forEach(function(v) {
for(var k in v) {
if(k.indexOf(row_identifier) > 0) filtered.push(v);
}
});
return filtered;
}
console.log(getRow(1));
console.log(getRow(2))
console.log(getRow(3))
}
testQA();
推荐阅读
- tfs - 如何将 Visual Studio 2019 连接到 TFS?
- sharepoint-2013 - 未经许可的用户可以看到没有要显示的项目的列表,而不是抱歉,此站点尚未与您共享。在 shrepoint
- python - PRINT 中的 IF 语句未对完整子句应用条件
- button - 在odoo中单击一次后如何更改按钮的颜色并使其禁用?
- sql - 在 SQL Server 中将总计值拆分为两列
- java - 双引号 ("") 与整数连接的目的
- python - 开发一个没有 GUI 的程序
- node.js - MongoDB 未使用 graphql 在节点 js 中更新
- java - Firestore 文档中引用类型的用例是什么以及如何在 android 应用程序中专门使用它?
- powershell - 将目录中的文件列表添加到数组列表