将对象推入对象数组 - Typescript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43151527/
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
Pushing objects into Object Array - Typescript
提问by Bromox
Having trouble with some code here. Building an application but stumbling over how to work with array objects. I provided the code above NgInit{}. And provided the TS error I am getting in Xcode.
这里有一些代码有问题。正在构建一个应用程序,但在如何使用数组对象方面遇到了困难。我提供了上面 NgInit{} 的代码。并提供了我在 Xcode 中遇到的 TS 错误。
Full component
完整组件
import { Component, OnInit } from '@angular/core';
import { Ticker } from '../TickerType';
import { ConversionService } from '../Conversion.service';
import {Observable} from 'rxjs/Rx';
import {resultsType} from './resultsTypeInterface';
@Component({
selector: 'app-contents',
templateUrl: './contents.component.html',
styleUrls: ['./contents.component.scss']
})
export class ContentsComponent implements OnInit{
//Will hold the data from the JSON file
// Variables for front end
cryptoSelected : boolean = false;
regSelected : boolean = false;
step2AOptions : any[] = [
{name: "make a selection..."},
{name: "Bitcoin"},
{name: "DASH"},
{name: "Etherium"}
]; // step2AOptions
step2BOptions : any[] = [
{name: "make a selection..."},
{name: "CAD"},
{name: "USD"}
]; // step2BOptions
step2Selection: string;
holdings: number = 10;
coins: any[] = ["BTC_ETH", "BTC_DASH"];
ticker: Ticker[];
coinResults: resultsType[] =[];
currencyExchange:any[] = [];
constructor( private conversionService: ConversionService ) {
}
Error
错误
Argument of type '{ name: string; amount: any; }[]' is not assignable to parameter of type 'resultsType'.
Property 'name' is missing in type '{ name: string; amount: any; }[]'.
This is happening in the code below. What I am trying to do is push this objects into an Object Array so I can access properties of like.
这发生在下面的代码中。我想要做的是将此对象推送到对象数组中,以便我可以访问类似的属性。
console.log(coinsResults[0].name);
Code
代码
ngOnInit(){
this.conversionService.getFullTicker().subscribe((res) => {this.ticker = res;
for(var j = 0; j<= this.coins.length-1; j++)
{
var currencyName: string = this.coins[j];
if(this.ticker[currencyName])
{
var temp = [{name: currencyName, amount: this.ticker[currencyName].last} ]
this.coinResults.push(temp)
}
}//end the for loop
}); //end the subscribe function
this.conversionService.getFullCurrencyExchange().subscribe( (res) => {this.currencyExchange = res["rates"]
});
console.log(this.coinResults);
}// End OnInit
回答by weirdan
coinResults
is declared as an array of resultsType
, so it's push
method would only accept arguments of resultsType
type. However, you're trying to push an arrayof resultsType
to coinResults
(note the square brackets):
coinResults
被声明为一个数组resultsType
,所以它的push
方法只接受resultsType
类型的参数。但是,你要推一个数组的resultsType
来coinResults
(注意括号):
// temp is resultsType[]
var temp = [{name: currencyName, amount: this.ticker[currencyName].last} ]
// but coinResults.push() accept only resultsType
this.coinResults.push(temp)
Loose the square brackets around the object literal in var temp=...
line.
在var temp=...
一行中松开对象文字周围的方括号。
回答by holi-java
you can't pass an array to Array.push(array)
since Array.push signature defined as reset parameters push(...items: T[])
, use Array.push(item) instead.
Array.push(array)
由于 Array.push 签名定义为重置参数push(...items: T[])
,因此您无法将数组传递给它,请改用 Array.push(item) 。
var temp = {name: currencyName, amount: this.ticker[currencyName].last};
this.coinResults.push(temp);
OR using spread operator:...
或使用扩展运算符:...
var temp = [{name: currencyName, amount: this.ticker[currencyName].last} ];
this.coinResults.push(...temp);