Home  >  Forum  >  Angular
Post New Query

How to fetch values from 2 arrays (json array) in angular?



Asked on December 28, 2017
I have following json array

[

[{"pk_location_id":1,"location_name":"India","location_phone":"99999"},
{"pk_location_id":2,"location_name":"karnakata","location_phone":"9"},
{"pk_location_id":3,"location_name":"hubli","location_phone":"9"}],

["","/[India]","/[karnakata]/[India]"]

]

I want to fetch location name (which is in first array) and location path (which is in second array).

Can any one help me out.





Replied on December 28, 2017
Create a method to access JSON data:

url = ".....";

getData(): Observable<any> {
       return this.http.get(this.url).map(res => res.json());
}

Now fetch location:

this.getData().subscribe(
       res => {
 let data = res[0];
 console.log(data[0]['location_name']);
 console.log(data[1]['location_name']);
 console.log(data[2]['location_name']);
       }
   );



Replied on December 28, 2017
If you want to access location name and path both, you can do in following way.

       this.getData().subscribe(
   res => {
   let location = res[0];
   console.log(location[0]['location_name']);
   console.log(location[1]['location_name']);
   console.log(location[2]['location_name']);
   
   let path = res[1];
   console.log(path[0]);
   console.log(path[1]);
   console.log(path[2]);

   }
   );



Replied on December 28, 2017
@Anupam thank you for the reply. I am trying to display the data in ngx-datatable 

My code in component.ts is as follows:

getAllLocations() {
// alert("comming in component ts");
console.log("comming in component ts");
this.LocationService.getAllLocations()
.subscribe
(data => {
// push our inital complete list
this.rows = data;
}
);
}

My code in html is as follows:

<ngx-datatable
class="material"
[rows]="rows"
[columnMode]="'force'"
[headerHeight]="50"
[footerHeight]="50"
[rowHeight]="50"
[scrollbarV]="true"
>
<ngx-datatable-column name="LocationName">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.location_name}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="LocationCode">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.location_code}}
</ng-template>
</ngx-datatable-column>
<ngx-datatable-column name="Path">
<ng-template let-row="row" ngx-datatable-cell-template>
{{row.path}}
</ng-template>
</ngx-datatable-column>
</ngx-datatable>

How do i print data in data table?

Thanks & Regards

Anita Patil


Write Answer







©2019 concretepage.com | Privacy Policy | Contact Us