Javascript 将结果保存在http.get()之后的数组中
我使用http.get()从api检索对象数组。每个项中只有一个属性:“number”。我希望该属性存储在我的app.component中的数组中。然后,我将使用该数组作为使用chart.js的图表的数据,目前效果良好。这是一天多的时间,我正在努力,但不知道如何做到这一点。你能告诉我吗 app.component.tsJavascript 将结果保存在http.get()之后的数组中,javascript,arrays,angular,http-get,angular-http,Javascript,Arrays,Angular,Http Get,Angular Http,我使用http.get()从api检索对象数组。每个项中只有一个属性:“number”。我希望该属性存储在我的app.component中的数组中。然后,我将使用该数组作为使用chart.js的图表的数据,目前效果良好。这是一天多的时间,我正在努力,但不知道如何做到这一点。你能告诉我吗 app.component.ts import { Component } from '@angular/core'; import { Http, Response } from '@angular/http
import { Component } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import 'rxjs/Rx';
import * as moment from 'moment';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'app';
nrs: any;
count: any;
public listNrs: Array<number> = [];
private apiAllUrl: string = 'http://localhost:3000/api/all';
private apiCountUrl: string = 'http://localhost:3000/api/count';
//private apiBothUrl: string = 'http://localhost:3000/api/both';
constructor(private http:Http) {
let now = moment.locale("it");
}
ngOnInit() {
this.getAllNrs();
this.getCountNrs();
this.getDataNumbers();
//this.getCount();
//this.loadData();
}
getAllNrs() {
this.http.get(this.apiAllUrl)
.map((res:Response) => res.json())
.subscribe(
data => { this.nrs = data},
err => console.error(err),
() => console.log(this.nrs)
);
}
getCountNrs(){
this.http.get(this.apiCountUrl)
.map((res:Response) => res.json())
.subscribe(
data => { this.count = data},
err => console.error(err),
() => console.log(this.count)
);
}
getDataNumbers(){
}
//tried using jsonp
/*loadData() {
this.jsonp.get(this.apiAllUrl)
.map(res => res.json())
.subscribe(data => console.log(data));
}*/
/*getCount(){
this.getAllNrs();
for(let nr of this.nrs){
console.log(this.nrs.nr[1]);
}
}*/
newDate(days) {
return moment().add(days, 'days');
}
public lineChartData:Array<any> = [
{data: [4, 5, 56, -23, 4,], label: 'Indexer meta served links'},
];
public lineChartData1:Array<any> = [
{data: [-23, 4, 4, 5, 56], label: 'Indexer served links'},
];
public lineChartData2:Array<any> = [
{data: [4, 5, 4, 56, -23], label: 'Real-Time served links'},
];
public lineChartLabels:Array<any> = ['1 Lug 2017', '2 Lug 2017', '3 Lug 2017', '4 Lug 2017', '5 Lug 2017'];
public lineChartOptions:any = {
responsive: true,
/*
scales: {
xAxes: [{
type: 'time',
time: {
displayFormats: {
'millisecond': 'DD MMM',
'second': 'DD MMM',
'minute': 'DD MMM',
'hour': 'DD MMM',
'day': 'DD MMM',
'week': 'DD MMM',
'month': 'DD MMM',
'quarter': 'DD MMM',
'year': 'DD MMM',
}
}
}],
},
*/
};
public lineChartColors:Array<any> = [
{ // grey
backgroundColor: 'rgba(255,55,55,0.2)',
borderColor: 'rgba(255,55,55,1)',
pointBackgroundColor: 'rgba(255,55,55,1)',
pointBorderColor: '#fff',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'rgba(255,55,55,0.8)'
},
];
public lineChartLegend:boolean = true;
public lineChartType:string = 'line';
// events
public chartClicked(e:any):void {
console.log(e);
}
public chartHovered(e:any):void {
console.log(e);
}
}
getAllNrs()
获取对象数组,其中每个对象都有一个名为number的属性
getAllCount()
获取对象数组,其中每个对象都有一个名为count的属性
问题是,即使我尝试将其映射到一个数组中,它也会告诉我this.nrs
未定义且无法获取其长度(错误:无法读取未定义的的属性“length”)。你知道为什么会这样吗
如果你还需要什么,请告诉我,我会提供的。
谢谢 您可以使用将对象数组
转换为字符串数组(或任何形式)
var arrOfObj=[
{
电话:100
},{
电话:200
},{
电话:300
},{
电话:400
},{
电话:500
},{
电话:600
}
];
var arrOfStr=arrOfObj.map(功能(项目){
返回项目编号;
});
控制台日志(arrOfStr)代码>从您的注释中,您需要初始化构造函数中的所有属性,否则它将得到未定义的
。Typescript无法神奇地知道它的默认值,即使您给它一个类型
export class AppComponent {
title = 'app';
nrs: any;
count: any;
public listNrs: Array<number> = [];
constructor(private http: Http) {
let now = moment.locale("it");
//initialize your values.
this.nrs = [];
this.count = 0;
}
//other codes
}
也许你需要告诉我们什么是getAllNrs()
做的,什么是getCountNrs()
,最重要的是,你的问题是什么?您在getDataNumbers()
或什么位置被阻止?到底哪些数据需要排序?@CozyAzure-done。问题就到最后了。谢谢你的回复,如果你还需要什么,请告诉我!谢谢你的回复。但是,当我尝试将this.nrs
映射到数组中时,程序告诉我它无法读取未定义的的属性“length”。你知道它为什么这样做吗?@giovannipechio消息很清楚,它说this.nrs是未定义的。你能确认吗?@giovannipechio我的意思是你必须确认为什么这个.nrs是未定义的:-)顺便问一下,这里与哪个http.get相关getAllNrs
或getCountNrs
?您可以直接在http.get().map中执行Array.map
部分。哦,对不起。。。我两个都需要,但如果你能告诉我如何为其中一个做,我会为另一个做。谢谢你,很抱歉给了我一个愚蠢的回答before@GiovanniPecchio看来你已经找到解决办法了。虽然它几乎和我的一样,但它很好去。祝你好运
export class AppComponent {
title = 'app';
nrs: any;
count: any;
public listNrs: Array<number> = [];
constructor(private http: Http) {
let now = moment.locale("it");
//initialize your values.
this.nrs = [];
this.count = 0;
}
//other codes
}
getAllNrs() {
this.http.get(this.apiAllUrl)
.map((res: Response) => res.json())
.subscribe(
data => {
this.nrs = data.map(nrObj => nrObj.number)
},
);
}