javascript - Angular : loop over object data loaded from a webservice -
i'm loading data form external webservice. my code looks :
my service ( load restfull data):
import {injectable} '@angular/core'; import {http} '@angular/http'; import 'rxjs/add/operator/map'; @injectable() export class httpservice { constructor(private http: http) {} loaddataitems(url: string) { return this.http.get(url) .map(data => { data.json(); return data.json(); }); } }
my component (where injecting previous service display data):
import {component, oninit} '@angular/core'; import 'rxjs/add/operator/map' import {httpservice} '../http.service'; import * $ 'jquery'; import * highcharts 'highcharts'; @component({ selector: 'app-list', templateurl: './list.component.html', styleurls: ['./list.component.css'], providers: [httpservice] }) export class listcomponent implements oninit { dataitems: any; nb = 0; private dataurl = 'http://localhost:3000/list'; // url web api constructor(private dataserver: httpservice ) {} ngoninit() { this.dataserver.loaddataitems(this.dataurl).subscribe( data => { this.dataitems = data; console.log(this.dataitems); } ); (let = 0 ; < this.dataitems.length ; i++) { if (this.dataitems.g = 'chrome') { this.nb++ } } // build chart highcharts.chart('chart2', { chart: { plotbackgroundcolor: null, plotborderwidth: null, plotshadow: false, type: 'pie' }, title: { text: 'browser market shares january, 2015 may, 2015' }, tooltip: { pointformat: '{series.name}: <b>{point.percentage:.1f}%</b>' }, plotoptions: { pie: { allowpointselect: true, cursor: 'pointer', datalabels: { enabled: false }, showinlegend: true } }, series: [{ name: 'brands', colorbypoint: true, data: [{ name: 'microsoft internet explorer', y: 56.33 }, { name: 'chrome', y: 24.03, sliced: true, selected: true }, { name: 'firefox', y: 10.38 }, { name: 'safari', y: 4.77 }, { name: 'opera', y: 0.91 }, { name: 'proprietary or undetectable', y: 0.2 }] }] }); } }
my purpose loop of items of received object data , need count data specific attribute :
if (this.dataitems.g = 'chrome')
but got error indicating not defined :
this.dataitems.length
i won't filter ngfor , ngif , because wanna catch counted value in ts file , use in highchart widget composant .
any ideas ??
for count or filter can use filter... long time no see syntax may wrong...
var query = myarray.filter( function(element){ return element.propert == neededvalue && element.property == othervalue; } ); return query?query.length:0;
good luck
Comments
Post a Comment