如何从Typescript中的字典界面获取数据?

时间:2016-07-02 18:19:39

标签: dictionary typescript

我已声明我的界面

export interface IBookBorrower {
  _id?: string
  name?: string
  age?: number
  [books: string]: any
}

当我获取数据时,我确实喜欢这个

 for (var bookId in this.borrower) 
   console.log(bookId, this.borrower[bookId]))

我收到的所有数据包括我不想要的_id,姓名和年龄, 我怎样才能在" books"中获取数据? ?

1 个答案:

答案 0 :(得分:2)

Typescript只是带有类型的javascript,所以你要求的东西(迭代只是同一个对象接口的一部分)是行不通的。

你需要在借阅者对象中创建自己的属性,无论如何它都可能更干净。

export interface IBookBorrower {
  _id?: string
  name?: string
  age?: number
  books: { [id: string]: any }
}

 for (var bookId in this.borrower.books) 
   console.log(bookId, this.borrower[bookId]))