代码之家  ›  专栏  ›  技术社区  ›  TheUnreal

角度2声明对象数组

  •  50
  • TheUnreal  · 技术社区  · 9 年前

    我有以下表达式:

    public mySentences:Array<string> = [
        {id: 1, text: 'Sentence 1'},
        {id: 2, text: 'Sentence 2'},
        {id: 3, text: 'Sentence 3'},
        {id: 4, text: 'Sentenc4 '},
    ];
    

    string 而是包含一个对象列表。如何删除数组以包含对象列表?

    *没有一个新的组件来声明似乎是浪费的句子的a类

    7 回复  |  直到 7 年前
        1
  •  101
  •   martin    8 年前

    我想你用的是打字稿。

    要特别小心,您可以定义您的 type 作为需要匹配特定接口的对象数组:

    type MyArrayType = Array<{id: number, text: string}>;
    
    const arr: MyArrayType = [
        {id: 1, text: 'Sentence 1'},
        {id: 2, text: 'Sentence 2'},
        {id: 3, text: 'Sentence 3'},
        {id: 4, text: 'Sentenc4 '},
    ];
    

    或不定义自定义类型的简短语法:

    const arr: Array<{id: number, text: string}> = [...];
    
        2
  •  28
  •   micronyks    5 年前
    public mySentences:Array<Object> = [
        {id: 1, text: 'Sentence 1'},
        {id: 2, text: 'Sentence 2'},
        {id: 3, text: 'Sentence 3'},
        {id: 4, text: 'Sentenc4 '},
    ];
    

    或者更确切地说,

    export interface type{
        id:number;
        text:string;
    }
    
    public mySentences:type[] = [
        {id: 1, text: 'Sentence 1'},
        {id: 2, text: 'Sentence 2'},
        {id: 3, text: 'Sentence 3'},
        {id: 4, text: 'Sentenc4 '},
    ];
    
        3
  •  14
  •   Ezio    8 年前

    1. 创建表示数据模型的类

      export class Data{
          private id:number;
          private text: string;
      
          constructor(id,text) {
              this.id = id;
              this.text = text;
          }
      
    2. 在组件类中,您创建了一个类型为 Data 并在收到来自API或正在使用的任何数据源的响应时填充此数组

      export class AppComponent {
          private search_key: string;
          private dataList: Data[] = [];
      
          getWikiData() {
             this.httpService.getDataFromAPI()
              .subscribe(data => {
                this.parseData(data);
              });
           }
      
          parseData(jsonData: string) {
          //considering you get your data in json arrays
          for (let i = 0; i < jsonData[1].length; i++) {
               const data = new WikiData(jsonData[1][i], jsonData[2][i]);
               this.wikiData.push(data);
          }
        }
      }
      
        4
  •  9
  •   Al Fahad Anjan Kumar GJ    7 年前

    首先,生成一个 界面

    假设您正在使用 TypeScript & Angular CLI ,可以使用以下命令生成一个

    ng g interface car

    然后设置其属性的数据类型

    // car.interface.ts
    export interface car {
      id: number;
      eco: boolean;
      wheels: number;
      name: string;
    }
    

    现在可以在所需的类中导入接口。

    import {car} from "app/interfaces/car.interface";

    this.car.push({
      id: 12345,
      eco: true,
      wheels: 4,
      name: 'Tesla Model S',
    });
    

    关于接口的更多信息:

    接口是TypeScript工件,它不是ECMAScript的一部分。接口是一种根据参数及其类型在函数上定义约定的方法。除了函数之外,接口还可以与Class一起用于定义自定义类型。 接口是抽象类型,它不像类那样包含任何代码。它只定义API的“签名”或形状。在编译期间,接口不会生成任何代码,它仅由Typescript用于开发期间的类型检查- https://angular-2-training-book.rangle.io/handout/features/interfaces.html

        5
  •  3
  •   nick    6 年前
    public mySentences:Array<any> = [
        {id: 1, text: 'Sentence 1'},
        {id: 2, text: 'Sentence 2'},
        {id: 3, text: 'Sentence 3'},
        {id: 4, text: 'Sentenc4 '},
    ];
    
    OR
    
    public mySentences:Array<object> = [
        {id: 1, text: 'Sentence 1'},
        {id: 2, text: 'Sentence 2'},
        {id: 3, text: 'Sentence 3'},
        {id: 4, text: 'Sentenc4 '},
    ];
    
        6
  •  2
  •   JJJ Sergey    6 年前

    数据类型: array_name:datatype[]=[]; 示例字符串: users:string[]=[];

    对于对象数组:

    对象类型: object_name:objecttype[]=[{}]; 示例用户: Users:user[]=[{}];

    如果在某些情况下它在绑定中未定义,请确保在 Oninit() .

        7
  •  1
  •   Annas    6 年前
    type NumberArray = Array<{id: number, text: string}>;
    
    const arr: NumberArray = [
        {id: 0, text: 'Number 0'},
        {id: 1, text: 'Number 1'},
        {id: 2, text: 'Number 2'},
        {id: 3, text: 'Number 3 '},
        {id: 4, text: 'Number 4 '},
        {id: 5, text: 'Number 5 '},
    ];
    
    推荐文章