【发布时间】:2018-02-10 05:29:03
【问题描述】:
我有一个这样的对象数组:
questionConfig = [{
section: 'base',
key: 'firstName',
label: 'First name',
type: 'text',
fieldType: 'input'
required: true,
},
{
section: 'dynamic'
key: 'emailAddress',
label: 'Email',
type: 'email',
fieldType: 'input',
required: true,
},
{
key: 'primaryPhone',
label: ' primary phone',
type: 'phone',
fieldType: 'input'
},
{
key: 'something',
label: 'dgfdsfgds',
options: [
{key: 1, value: 'dsfgdsg'},
{key: 2, value: 'dfgdsfg'},
{key: 3, value: 'dsfgdsgds'},
{key: 4, value: 'dsfgdgsf'}
],
fieldType: 'dropdown'
}]
如何过滤此数组以获取 section = 'base' 的对象?
如果尝试过
let questionBaseSection = this.questionConfig.filter(question => question.section === 'base' )
并得到错误: 类型 '{ section: string; 上不存在属性 'section'键:字符串;标签:字符串;必需:布尔值; minLength:数字; minLengthErr...'。 类型 '{ key: string; 上不存在属性 'section'标签:字符串;类型:字符串;字段类型:字符串;必需:布尔值; minLength: 麻木...'。
【问题讨论】:
-
questionConfig是如何声明的? -
您的数组中似乎有一个项目没有
section属性。第二个错误说明了这一点。检查这个。 -
只有你在上面看到的。这不正确吗?
-
TypeScript 的重点是使用类型。如果你不定义类型,我建议你使用普通的 ECMAScript。
-
正如所写,这应该没有什么问题。错误说 Property 在类型上不存在,所以您是否随时将任何其他对象添加到数组中?
标签: typescript ecmascript-6 angular-cli