Arrays 角度2声明对象数组
我有以下的表达:Arrays 角度2声明对象数组,arrays,angular,typescript,Arrays,Angular,Typescript,我有以下的表达: public mySentences:Array<string> = [ {id: 1, text: 'Sentence 1'}, {id: 2, text: 'Sentence 2'}, {id: 3, text: 'Sentence 3'}, {id: 4, text: 'Sentenc4 '}, ]; public mycentenses:Array=[ {id:1,文本:“第1句”}, {id:2,文本:“第2句”}, {i
public mySentences:Array<string> = [
{id: 1, text: 'Sentence 1'},
{id: 2, text: 'Sentence 2'},
{id: 3, text: 'Sentence 3'},
{id: 4, text: 'Sentenc4 '},
];
public mycentenses:Array=[
{id:1,文本:“第1句”},
{id:2,文本:“第2句”},
{id:3,文本:“第3句”},
{id:4,文本:'Sentenc4'},
];
这不起作用,因为我的数组不是string
类型,而是包含一个对象列表。如何删除数组以包含对象列表
*如果没有一个新的组件来声明一个句子的类,那么这个句子似乎是一种浪费我假设您使用的是typescript
public mySentences:Array<Object> = [
{id: 1, text: 'Sentence 1'},
{id: 2, text: 'Sentence 2'},
{id: 3, text: 'Sentence 3'},
{id: 4, text: 'Sentenc4 '},
];
要特别小心,您可以将类型定义为需要匹配特定接口的对象数组:
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 '},
];
类型MyArrayType=Array;
常量arr:MyArrayType=[
{id:1,文本:“第1句”},
{id:2,文本:“第2句”},
{id:3,文本:“第3句”},
{id:4,文本:'Sentenc4'},
];
或不定义自定义类型的简短语法:
const arr: Array<{id: number, text: string}> = [...];
常量arr:Array=[…];
首先,生成一个接口
假设您正在使用&,您可以使用以下命令生成一个
ng g接口车
然后设置其属性的数据类型
// car.interface.ts
export interface car {
id: number;
eco: boolean;
wheels: number;
name: string;
}
现在可以在所需的类中导入接口
从“app/interfaces/car.interface”导入{car}代码>
并通过推送阵列中的项目来更新汽车对象的集合/阵列
this.car.push({
id: 12345,
eco: true,
wheels: 4,
name: 'Tesla Model S',
});
有关接口的更多信息:
接口是TypeScript工件,它不是ECMAScript的一部分。接口是一种根据参数及其类型定义函数契约的方法。除了函数外,接口还可以与类一起用于定义自定义类型。
接口是一种抽象类型,它不像类那样包含任何代码。它只定义API的“签名”或形状。在编译期间,接口不会生成任何代码,它仅由Typescript用于开发期间的类型检查。-
如果要存储来自外部API或DB的数据,另一种特别有用的方法是:
创建一个表示数据模型的类
export class Data{
private id:number;
private text: string;
constructor(id,text) {
this.id = id;
this.text = text;
}
在组件类中,创建一个类型为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);
}
}
}
导出类AppComponent{
私钥:字符串;
私有数据列表:数据[]=[];
getWikiData(){
this.httpService.getDataFromAPI()
.订阅(数据=>{
这个.parseData(数据);
});
}
parseData(jsonData:string){
//考虑到您在json数组中获取数据
for(设i=0;i
数据类型:array\u名称:数据类型[]=[]代码>
示例字符串:用户:字符串[]=[]代码>
对于对象数组:
Objecttype:object\u name:Objecttype[]=[{}]代码>
示例用户:用户:用户[]=[{}]代码>
如果在某些情况下它在绑定中未定义,请确保在Oninit()
上初始化它;
常数arr:NumberArray=[
{id:0,文本:'Number 0'},
{id:1,文本:'Number 1'},
{id:2,文本:'Number 2'},
{id:3,文本:'Number 3'},
{id:4,文本:'Number 4'},
{id:5,文本:'Number 5'},
];
公共mycentenses:Array=[
{id:1,文本:“第1句”},
{id:2,文本:“第2句”},
{id:3,文本:“第3句”},
{id:4,文本:'Sentenc4'},
];
或
公共mySentences:Array=[
{id:1,文本:“第1句”},
{id:2,文本:“第2句”},
{id:3,文本:“第3句”},
{id:4,文本:'Sentenc4'},
];
答案是有效的,但如果您有一个对象的多个属性怎么办?这不是一种标准的方式@Martin。在Angular2文档中,您可能会发现这样的声明,但它不是使用typescript的标准声明。如何将dataList
设置为5的大小?它应该是this.cars.push(),而不是this.cars.push(),或者任何局部变量的名称。在代码中说明这是如何声明的是有意义的,因为只有到那时问题才会得到回答。成功了。谢谢。但是现在我不知道如何在这里推广价值观。我想推送一组对象,如下所示:this.mycentenses.push(res)
。我犯了个错误。类型为“type[]”的参数不能分配给类型为“type”的参数。。。。
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);
}
}
}
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 '},
];
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 '},
];