[英]Pushing objects into Object Array - Typescript
Having trouble with some code here. 在这里遇到一些代码问题。 Building an application but stumbling over how to work with array objects.
正在构建应用程序,但在如何使用数组对象方面遇到了困难。 I provided the code above NgInit{}.
我在NgInit {}上方提供了代码。 And provided the TS error I am getting in Xcode.
并提供了Xcode中出现的TS错误。
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 ) {
}
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.
我想做的是将这个对象推入一个对象数组,以便可以访问like的属性。
console.log(coinsResults[0].name);
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
coinResults
is declared as an array of resultsType
, so it's push
method would only accept arguments of resultsType
type. coinResults
被声明为resultsType
的数组,因此它的push
方法将只接受resultsType
类型的参数。 However, you're trying to push an array of resultsType
to coinResults
(note the square brackets): 但是,您尝试将
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=...
行中对象文字的方括号。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.