最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pushing objects into Object Array - Typescript - Stack Overflow

programmeradmin2浏览0评论

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.

Full ponent

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: './contentsponent.html',
  styleUrls: ['./contentsponent.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

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.

Full ponent

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.ponent.html',
  styleUrls: ['./contents.ponent.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
Share Improve this question asked Apr 1, 2017 at 0:40 BromoxBromox 7072 gold badges13 silver badges35 bronze badges 1
  • update your post with the resultsType code – Aravind Commented Apr 1, 2017 at 0:42
Add a ment  | 

2 Answers 2

Reset to default 2

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 array of resultsType to coinResults (note the square brackets):

// 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.

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.

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);
发布评论

评论列表(0)

  1. 暂无评论