Arrays 使用键值将项目推送到typescrip中的数组
我想将一个数组推送到另一个数组,但结果生成的结果不正确Arrays 使用键值将项目推送到typescrip中的数组,arrays,angular,typescript,Arrays,Angular,Typescript,我想将一个数组推送到另一个数组,但结果生成的结果不正确 let pusheditems:any[] = []; pusheditems.push(this.yesvalue); pusheditems.push(this.selectedtruck); 稍后当我console.log(pusheditems) 我得到一个类型为的数组 array(0->yes array, 1->select truck array) 我想要的是将索引值0,1更改为字符串,如yes,truck
let pusheditems:any[] = [];
pusheditems.push(this.yesvalue);
pusheditems.push(this.selectedtruck);
稍后当我console.log(pusheditems)
我得到一个类型为的数组
array(0->yes array, 1->select truck array)
我想要的是将索引值0,1更改为字符串,如yes,truck
所以我希望得到
array(yes->yes array, truck->select truck array)
我也试过了
pusheditems.push({yes:this.yesvalue}); //adding yes
pusheditems.push({truck:this.selectedtruck}); //adding truck
但这不起作用
价值观
this.yesvalues and this.selectedtruck are also arrays
我需要进一步添加什么呢?您试图实现的是创建对象,而不是数组 你可以这样做:
let pusheditems = {};
pusheditems[this.yesvalue] = this.selectedtruck;
在Typescript中,数组只能具有number类型的键。您需要使用
字典
对象,如:
let pusheditems: { [id: string]: any; } = {}; // dictionary with key of string, and values of type any
pusheditems[this.yesvalue] = this.selectedtruck; // add item to dictionary