Geo*_*off 2 arrays typescript angular
我想将一个数组推送到另一个数组,但结果会产生不正确的结果
let pusheditems:any[] = [];
pusheditems.push(this.yesvalue);
pusheditems.push(this.selectedtruck);
Run Code Online (Sandbox Code Playgroud)
后来当我 console.log(pusheditems)
我得到了一个类型的数组
array(0->yes array, 1->select truck array)
Run Code Online (Sandbox Code Playgroud)
我们正在寻找的是将0的索引值更改为yes,truck等字符串
所以我希望得到
array(yes->yes array, truck->select truck array)
Run Code Online (Sandbox Code Playgroud)
我也试过了
pusheditems.push({yes:this.yesvalue}); //adding yes
pusheditems.push({truck:this.selectedtruck}); //adding truck
Run Code Online (Sandbox Code Playgroud)
但这不起作用
的价值观
this.yesvalues and this.selectedtruck are also arrays
Run Code Online (Sandbox Code Playgroud)
我需要进一步添加什么
在Typescript中,数组只能包含类型为number的键.你需要使用Dictionary对象,比如:
let pusheditems: { [id: string]: any; } = {}; // dictionary with key of string, and values of type any
pusheditems[this.yesvalue] = this.selectedtruck; // add item to dictionary
Run Code Online (Sandbox Code Playgroud)
您试图实现的目标是创建对象,而不是数组。
你能行的:
let pusheditems = {};
pusheditems[this.yesvalue] = this.selectedtruck;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14832 次 |
| 最近记录: |