Javascript 在 TypeScript 中声明一个数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38875051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:59:00  来源:igfitidea点击:

Declare an array in TypeScript

javascriptarraystypescript

提问by Nitzan Tomer

I'm having trouble either declaring or using a boolean array in Typescript, not sure which is wrong. I get an undefinederror. Am I supposed to use JavaScript syntax or declare a new Array object?

我在 Typescript 中声明或使用布尔数组时遇到问题,不确定哪个是错误的。我收到一个undefined错误。我应该使用 JavaScript 语法还是声明一个新的 Array 对象?

Which one of these is the correct way to create the array?

以下哪一种是创建数组的正确方法?

private columns = boolean[];
private columns = [];
private columns = new Array<boolean>();

How would I initialize all the values to be false?

我将如何将所有值初始化为 false?

How would I access the values, can I access them like, columns[i] = true;..?

我将如何访问这些值,我可以像columns[i] = true;……一样访问它们吗?

回答by Nitzan Tomer

Here are the different ways in which you can create an array of booleans in typescript:

以下是在打字稿中创建布尔数组的不同方法:

let arr1: boolean[] = [];
let arr2: boolean[] = new Array();
let arr3: boolean[] = Array();

let arr4: Array<boolean> = [];
let arr5: Array<boolean> = new Array();
let arr6: Array<boolean> = Array();

let arr7 = [] as boolean[];
let arr8 = new Array() as Array<boolean>;
let arr9 = Array() as boolean[];

let arr10 = <boolean[]> [];
let arr11 = <Array<boolean>> new Array();
let arr12 = <boolean[]> Array();

let arr13 = new Array<boolean>();
let arr14 = Array<boolean>();

You can access them using the index:

您可以使用索引访问它们:

console.log(arr[5]);

and you add elements using push:

然后使用 push 添加元素:

arr.push(true);

When creating the array you can supply the initial values:

创建数组时,您可以提供初始值:

let arr1: boolean[] = [true, false];
let arr2: boolean[] = new Array(true, false);

回答by Kayani

this is how you can create an array of boolean in TS and initialize it with false:

这是在 TS 中创建布尔数组并用 false 初始化它的方法:

var array: boolean[] = [false, false, false]

or another approach can be:

或另一种方法可以是:

var array2: Array<boolean> =[false, false, false] 

you can specify the type after the colon which in this case is boolean array

您可以在冒号后指定类型,在这种情况下是布尔数组

回答by Mark Macneil Bikeio

Specific type of array in typescript

打字稿中特定类型的数组

export class RegisterFormComponent 
{
     genders = new Array<GenderType>();   // Use any array supports different kind objects

     loadGenders()
     {
        this.genders.push({name: "Male",isoCode: 1});
        this.genders.push({name: "FeMale",isoCode: 2});
     }
}

type GenderType = { name: string, isoCode: number };    // Specified format

回答by snehal badhe

let arr1: boolean[] = [];

console.log(arr1[1]);

arr1.push(true);

回答by Arghya C

One way of declaring a typed array in TypeScriptis

声明类型化数组的一种方法TypeScript

const booleans = new Array<Boolean>();

// or, if you have values to initialize 
const booleans: Array<Boolean> = [true, false, true];
const valFalse = booleans[1];