Angular2 在找到合适的匹配项后显示 JSON 中选定下拉值的特定问题
Angular2 displaying a particular Questions from the JSON for the selected dropdown values after finding a proper match
我有一个下拉列表,其中包含 1 和 2 等数值。在我的 JSON 中,我有一个 "filters"
属性,它是一个数组。要求是:
每当我 select 下拉菜单中的任何值 — 假设我在下拉列表中 selected 2 — 然后我想检查 selected 值是否等于"filters" 数组中的任何 "answer" 属性,其值与 selected 值相同,即 2。
如果多个对象具有相同的答案值,则应检查 "code"
是否与 "filters` array for the selected match. If yes, then the respective "displayKey 的 "questionCode" 相匹配。
我的JSON:
{
"code": "abc",
"displayKey": "question1",
"filters": [{
"questionCode": "abc",
"answer": "2"
}],
"order": 9,
"industryList": {}
},
{
"code": "pqr23",
"displayKey": "question2",
"filters": [{
"questionCode": "gthy67",
"answer": "1"
}],
"order": 2,
"industryList": {}
},
{
"code": "abc",
"displayKey": "question3",
"filters": [{
"questionCode": "abc342",
"answer": "2"
}],
"order": 9,
"industryList": {}
}
在我的 .ts 文件中:
for(var i=0; i<=this.questions.filters.length;i++ ){
var questionsObj = this.questions.filters[i];
//psuedoCode
if(this.selectedValue == questionsObj.answer){
if(this.questions.code == questionsObj.questionCode ){
this.displayFilterCode = true;
}
else{
this.displayFilterCode = false;
}
}
}
我的 JSON 非常大,我将整个对象保存在一个 questions
对象中。我刚刚在这里添加了所需的位。
在我的 HTML:
<dropdown [(ngModel)] = "selectedValue"></dropdown>
我的下拉菜单在页面上正确填充,但不确定为什么没有显示相应的问题。任何人都可以让我知道我做错了什么以及如何显示问题。我不确定我是否遵循正确的方法,但我正在尝试这种方式。另外,我收到长度未定义的错误(不确定为什么)。由于 filters
是一个数组,因此 filters.length
应该有效。
好吧,您没有正确访问 json。
这是你应该尝试的
for(var i=0; i<=this.questions.length;i++ ){
var questionsObj = this.questions[i];
//now one question from question array is assigned to questionobj
if(this.selectedValue == questionsObj.filters[0].answer){// your answer is in questionsObj.filters[0].answer position
if(this.questions[i].filters[0].questionCode == questionsObj.filters[0].questionCode ){
this.displayFilterCode = true;
}
else{
this.displayFilterCode = false;
}
}
如果您仍然遇到任何错误,请告诉我。因为我不知道你从哪里得到 this.seletedValue。
我有一个下拉列表,其中包含 1 和 2 等数值。在我的 JSON 中,我有一个 "filters"
属性,它是一个数组。要求是:
每当我 select 下拉菜单中的任何值 — 假设我在下拉列表中 selected 2 — 然后我想检查 selected 值是否等于"filters" 数组中的任何 "answer" 属性,其值与 selected 值相同,即 2。
如果多个对象具有相同的答案值,则应检查 "code"
是否与 "filters` array for the selected match. If yes, then the respective "displayKey 的 "questionCode" 相匹配。
我的JSON:
{
"code": "abc",
"displayKey": "question1",
"filters": [{
"questionCode": "abc",
"answer": "2"
}],
"order": 9,
"industryList": {}
},
{
"code": "pqr23",
"displayKey": "question2",
"filters": [{
"questionCode": "gthy67",
"answer": "1"
}],
"order": 2,
"industryList": {}
},
{
"code": "abc",
"displayKey": "question3",
"filters": [{
"questionCode": "abc342",
"answer": "2"
}],
"order": 9,
"industryList": {}
}
在我的 .ts 文件中:
for(var i=0; i<=this.questions.filters.length;i++ ){
var questionsObj = this.questions.filters[i];
//psuedoCode
if(this.selectedValue == questionsObj.answer){
if(this.questions.code == questionsObj.questionCode ){
this.displayFilterCode = true;
}
else{
this.displayFilterCode = false;
}
}
}
我的 JSON 非常大,我将整个对象保存在一个 questions
对象中。我刚刚在这里添加了所需的位。
在我的 HTML:
<dropdown [(ngModel)] = "selectedValue"></dropdown>
我的下拉菜单在页面上正确填充,但不确定为什么没有显示相应的问题。任何人都可以让我知道我做错了什么以及如何显示问题。我不确定我是否遵循正确的方法,但我正在尝试这种方式。另外,我收到长度未定义的错误(不确定为什么)。由于 filters
是一个数组,因此 filters.length
应该有效。
好吧,您没有正确访问 json。 这是你应该尝试的
for(var i=0; i<=this.questions.length;i++ ){
var questionsObj = this.questions[i];
//now one question from question array is assigned to questionobj
if(this.selectedValue == questionsObj.filters[0].answer){// your answer is in questionsObj.filters[0].answer position
if(this.questions[i].filters[0].questionCode == questionsObj.filters[0].questionCode ){
this.displayFilterCode = true;
}
else{
this.displayFilterCode = false;
}
}
如果您仍然遇到任何错误,请告诉我。因为我不知道你从哪里得到 this.seletedValue。